Removing new lines from text file in order for text to appear on a single line using awk,tr or sed [duplicate] - linux

This question already has answers here:
How can I replace each newline (\n) with a space using sed?
(43 answers)
Closed 2 years ago.
How do we remove newlines in a test.txt file so that the text now appears on a single line using tr,awk or sed?
E.g
My name is mo
Learning linux
live in CAD
If I want that text to appear on one line and save it to a new file called passed.txt. What command should I run?

With awk:
awk '{ printf "%s",$0 }' file > passed.txt # print each line ($0) of file with no new lines
With tr:
tr -d '\n' < file > passed.txt # Use -d to delete new lines (\n)
xargs and printf is also an option:
xarg printf "%s" < file > passed.txt # Redirect file into printf and print each default space variable without new lines.
Output is redirected to passed.txt with each example

Related

How can I concatenate string in loop in linux bash? [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed last year.
I am new on coding in bash Linux and I have the following problem.
I'm trying to concatenate string in loop to create a path. I have a text file in which I stored some strings to use in the loop. I wrote this example just to show you the problem:
for bio in `cat /data/giordano/species_ranges/prova_bio.txt` # list of strings: "bio_01", "bio_02"...
do
echo /data/giordano/species_range/$bio.tif # concatenation
done
The result I expect would be:
/data/giordano/species_range/bio_01.tif
/data/giordano/species_range/bio_02.tif
/data/giordano/species_range/bio_03.tif
But what actually came out was:
.tifa/giordano/species_range/bio_01
.tifa/giordano/species_range/bio_02
.tifa/giordano/species_range/bio_03
/data/giordano/species_range/bio_04.tif
I really don't understand what kind of problem it is...
I suggest that awk would be simpler for this task. We use tr to remove the Cr line endings
~/tests/bash $ tr -d "\r" < data/giordano/species_range/proverbio.txt | awk '{ print "/data/giordano/species_range/" $0 ".tif"
> }'
/data/giordano/species_range/bio_1.tif
/data/giordano/species_range/bio_2.tif
/data/giordano/species_range/bio_3.tif
/data/giordano/species_range/bio_4.tif
Thank you to Charles Duffy for the improvements.
You probably have Windows line endings in your file, which contain an additional carriage return (\r). This makes the cursor go to the beginning of the line. You can remove the \rs from your file by piping to tr. Extend your first line like this:
for bio in `cat /data/giordano/species_ranges/prova_bio.txt | tr -d '\r'`

How can I use sed or grep to delete the first line of input if it contains a string? [duplicate]

This question already has answers here:
How do I match multiple addresses in sed?
(4 answers)
Closed 3 years ago.
I am redirecting input and have 2 fields that I extracted from the tail of an XML file, and I need to ignore the first line if it isn't the first of the 2 entries.
tail -n 327 ~/.local/share/recently-used.xbel | grep -e "<bookmark href=" -e "<mime:mime-type type="
Here is the output from that code, which is working fine, but the problem is that the first line is a
<mime:mime-type type="application/x-shellscript"/>
<bookmark href="file:///usr/local/bin/menu_manager.sh" added="2019-09-17T08:33:48Z" modified="2019-09-17T08:33:48Z" visited="2019-09-17T08:33:49Z">
<mime:mime-type type="application/x-shellscript"/>
I need to look at the first line, and if it contains the string
<mime:mime-type type=
then I need to remove that line and pass the rest of the lines on for the next processing step
I tried
sed '1/<mime:mime-type/d'
But is gives me an error:
sed: -e expression #1, char 2: unknown command: `/'
Try
sed '1{/<mime:mime-type/d}'
which uses a block {} which is only run on line 1, with the delete command in the block.
If you are OK with awk you can use this
awk 'NR!=1 || !/<mime:mime-type type=/'
This prints every line that is not the first line (NR!=1) or doesn't match the pattern (!/<mime:mime-type type=/). As there is no action specified, awk uses the default action print.

How to delete 1 or more matching line(s) while reading a file in bash script [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Combining two sed commands
(2 answers)
Linux, find replace on a folder of files using a list of items for replacement?
(1 answer)
Closed 4 years ago.
I want to read file using a bash script and delete line(s) which are matching with my specific scenario (line(s) starting with 'z').
my code works fine if the 'inputFile' contains only alphabetic characters.
but, if a line with 'specific characters of sed' (line eg : z-2.10.3.2 x/y/z F (&)[]+* ) then i got an error,(error : sed: -e expression #1, char 29: unterminated `y' command).
#!/bin/bash
inputFile="test.txt"
while IFS= read -r line
do
echo "$line"
if [[ $line == z* ]];
then
sed -i "/$line/d" $inputFile
fi
done < "$inputFile"
i want to delete 'z-2.10.3.2 x/y/z F (&)[]+*' kind of lines, how can i do this...?
As you mentioned you don't need line which has z*
Simply use grep -v
grep -vE "^[[:blank:]]*z" file
I have created one scenario where I have a file which contains
root#ubuntu:~/T/e/s/t# cat file
hello world
sample line 1
sample line 2 world
sample line 3
sample line 4
In my case, I want to remove the line contains "world"
root#ubuntu:~/T/e/s/t# grep -v "world" file
sample line 1
sample line 3
sample line 4
If you want you can redirect your output in another file.

How to add a character to the end of each line in a file in shell script [duplicate]

This question already has answers here:
sed edit file in place
(15 answers)
Closed 8 years ago.
I need a script that add a particular character to the end of each line . I am using the command
sed 's/$/ foo/' r.txt
It adds foo to end of each line in the file r.txt and displays in my terminal .
What do i need to do if i want to save this existing file with this new record appended after the end of each line .
To save to a new file:
sed 's/$/ foo/' r.txt > newfile.txt
To edit in place
sed -i 's/$/ foo/' r.txt

Bash to merge 2 consecutive lines in a file [duplicate]

This question already has answers here:
how can I combine these lines
(4 answers)
Closed 8 years ago.
I want convert this text on a given file:
87665
S
3243423
S
334243
N
...
to something like this:
87665,S
3243423,S
334243,N
...
I've been reading some similar questions, but it didn't work... is there a way to do this with a single line command in linux?
Thanks!
Using sed:
sed '$!N;s/\n/,/' filename
Using paste:
paste -d, - - < filename
paste would leave a trailing , in case the input has an odd number of lines.
Something like this might work for you:
$ awk 'NR%2{a=$0;next}{print a","$0}' file
87665,S
3243423,S
334243,N
To handle files with odd lines, you can do:
awk '{printf "%s%s", $0, NR%2?",":ORS}' file
Just for fun, a pure bash solution:
while IFS= read -r l1; do
read -r l2
printf '%s\n' "$l1${l2:+,$l2}"
done < file
If there's an odd number of lines, the last line will not have a trailing comma.

Resources