I have a string in the following format:
string1:string2:string3:string4:string5
I'm trying to use sed to split the string on : and print each sub-string on a new line. Here is what I'm doing:
cat ~/Desktop/myfile.txt | sed s/:/\\n/
This prints:
string1
string2:string3:string4:string5
How can I get it to split on each delimiter?
To split a string with a delimiter with GNU sed you say:
sed 's/delimiter/\n/g' # GNU sed
For example, to split using : as a delimiter:
$ sed 's/:/\n/g' <<< "he:llo:you"
he
llo
you
Or with a non-GNU sed:
$ sed $'s/:/\\\n/g' <<< "he:llo:you"
he
llo
you
In this particular case, you missed the g after the substitution. Hence, it is just done once. See:
$ echo "string1:string2:string3:string4:string5" | sed s/:/\\n/g
string1
string2
string3
string4
string5
g stands for global and means that the substitution has to be done globally, that is, for any occurrence. See that the default is 1 and if you put for example 2, it is done 2 times, etc.
All together, in your case you would need to use:
sed 's/:/\\n/g' ~/Desktop/myfile.txt
Note that you can directly use the sed ... file syntax, instead of unnecessary piping: cat file | sed.
Using \n in sed is non-portable. The portable way to do what you want with sed is:
sed 's/:/\
/g' ~/Desktop/myfile.txt
but in reality this isn't a job for sed anyway, it's the job tr was created to do:
tr ':' '
' < ~/Desktop/myfile.txt
Using simply tr :
$ tr ':' $'\n' <<< string1:string2:string3:string4:string5
string1
string2
string3
string4
string5
If you really need sed :
$ sed 's/:/\n/g' <<< string1:string2:string3:string4:string5
string1
string2
string3
string4
string5
This might work for you (GNU sed):
sed 'y/:/\n/' file
or perhaps:
sed y/:/$"\n"/ file
This should do it:
cat ~/Desktop/myfile.txt | sed s/:/\\n/g
If you're using gnu sed then you can use \x0A for newline:
sed 's/:/\x0A/g' ~/Desktop/myfile.txt
Related
I have bash variable:
VAR="This is \n what i want"
and file with following content:
asdf
zxcv
qwer
I would like to substitue every occurance of zxcv with value of $VAR (multiline text)
How to do it with awk or sed?
I tried:
sed -i 's/zxcv/'$VAR'/g' filename
sed -i "s/zxcv/$VAR/g" filename
sed -i "s/zxcv/$(VAR)/g" filename
sed -i "s/zxcv/$(($VAR))/g" filename
sed -i "s/zxcv/`echo -n $VAR`/g" filename
Using gnu-awk, you can do this by passing variable's value on command line:
var=$'This is \n what i want'
awk -v var="$var" -v w='zxcv' 'n=index($0, w){
$0 = substr($0, 1, n-1) var substr($0, n + length(w))} 1' file
asdf
This is
what i want
qwer
If search word appears in a separate line (as shown in question) then this command can simplified to this:
awk -v var="$var" -v w='zxcv' 'index($0, w)==1{$0 = var} 1' file
You can use GNU sed as well:
$ var="This is \n what i want"
$ sed "s/^zxcv/$var/" file
asdf
This is
what i want
qwer
The key is using double quotes versus single quotes so the value of $var is inserted into the sed replacement string.
(btw, as convention, use lower case in Bash for user variables.)
From comments: Ed Morton's version is way better by using string concatenation and only exposing the variable component to shell interpretation:
$ sed 's/^zxcv/'"$var"'/' file
Use that
I have file that contains below information
$ cat test.txt
Studentename:Ram
rollno:12
subjects:6
Highest:95
Lowest:65
Studentename:Krish
rollno:13
subjects:6
Highest:90
Lowest:45
Studentename:Sam
rollno:14
subjects:6
Highest:75
Lowest:65
I am trying place info of single student in single.
i.e My output should be
Studentename:Ram rollno:12 subjects:6 Highest:95 Lowest:65
Studentename:Krish rollno:13 subjects:6 Highest:90 Lowest:45
Studentename:Sam rollno:14 subjects:6 Highest:75 Lowest:65.
Below is the command I wrote
cat test.txt | tr "\n" " " | sed 's/Lowest:[0-9]\+/Lowest:[0:9]\n/g'
Above command is breaking line at regex Lowest:[0-9] but it doesn't print the pattern. Instead it is printing Lowest:[0-9].
Please help
Try:
$ sed '/^Studente/{:a; N; /Lowest/!ba; s/\n/ /g}' test.txt
Studentename:Ram rollno:12 subjects:6 Highest:95 Lowest:65
Studentename:Krish rollno:13 subjects:6 Highest:90 Lowest:45
Studentename:Sam rollno:14 subjects:6 Highest:75 Lowest:65
How it works
/^Studente/{...} tells sed to perform the commands inside the curly braces only on lines that start with Studente. Those commands are:
:a
This defines a label a.
N
This reads in the next line and appends it to the pattern space.
/Lowest/!ba
If the current pattern space does not contain Lowest, this tells sed to branch back to label a.
In more detail, /Lowest/ is true if the line contains Lowest. In sed, ! is negation so /Lowest/! is true if the line does not containLowest. Inba, thebstands for the branch command anda` is the label to branch to.
s/\n/ /g
This tells sed to replace all newlines with spaces.
Try this using awk :
awk '{if ($1 !~ /^Lowest/) {printf "%s ", $0} else {print}}' file.txt
Or shorter but more obfuscated :
awk '$1!~/^Lowest/{printf"%s ",$0;next}1' file.txt
Or correcting your command :
tr "\n" " " < file.txt | sed 's/Lowest:[0-9]\+/&\n/g'
Explanation: & is whats matched in the left part of substitution
Another possible GNU sed that doesn't assume Lowest is the last item:
sed ':a; N; /\nStudent/{P; D}; s/\n/ /; ba' test.txt
This might work for you (GNU sed):
sed '/^Studentename:/{:a;x;s/\n/ /gp;d};H;$ba;d' file
Use the hold space to gather up the fields and then remove the newlines to produce a record.
I am trying to delete the string '\r\n' from a file.
Using sed:
cat foo | sed -e 's/\015\012//'
does not seem to work.
tr -d '\015'
will delete a single character but I want to remove the string \015\012. Any suggestions?
If I can offer a perl solution:
$ printf "a\nb\r\nc\nd\r\ne\n" | perl -0777 -pe 's/\r\n//g' | od -c
0000000 a \n b c \n d e \n
0000010
The -0777 option causes the entire file to be slurped in as a single string.
What about:
sed ':a;N;$!ba;s/\r\|\n//g'
This is to remove any \r and \n characters. If you want the sequence \r\n, then use this:
sed ':a;N;$!ba;s/\r\n//g'
tuned from:
https://stackoverflow.com/a/1252191/520567
I am trying to match a line and use sed command to substitute it. Some thing like
aaa = 10
aaa =10
aaa=10
My sed regular expression should match all those patterns and should replace with something like bbb=5. I tried with
sed -i '/ *aaa *= */bbb=5'
But this is not properly working for all the patterns. Any help will be really appreciable.
sed -i 's/\s*aaa\s*=\s*[0-9]*/bbb=5/' input_file
cat a | sed -e '1s/aaa =10/bbb=10/' -e '2s/ aaa =10/bbb=10/' -e '3s/aaa=10/bbb=10/'
cat myfile | sed 's/\s*aaa\s*=\s*\(.*\)/bbb = \1/'
The \s character class matches both tab and space
I have a text file which contains some lines as the following:
ASDASD2W 3ASGDD12 SDADFDFDDFDD W11 ACC=PNO23 DFSAEFEA EAEDEWRESAD ASSDRE
AERREEW2 3122312 SDADDSADADAD W12 ACC=HH34 23SAEFEA EAEDEWRESAD ASEEWEE
A15ECCCW 3XCXXF12 SDSGTRERRECC W43 ACC=P11 XXFSAEFEA EAEDEWRESAD ASWWWW
ASDASD2W 3122312 SDAFFFDEEEEE SD3 ACC=PNI22 ABCEFEA EAEDEWRESAD ASWEDSSAD
...
I have to extract the substring between the '=' character and the following blank space for each line , i.e.
PNO23
HH34
P11
PNI22
I've been using the sed command but cannot figure out how to ignore all characters following the blank space.
Any help?
Use the right tool for the job.
$ awk -F '[= ]+' '{ print $6 }' input.txt
PNO23
HH34
P11
PNI22
Sorry, but have to add another one because I feel the existing answers are just to complicated
sed 's/.*=//; s/ .*//;' inputfile
This might work for you:
sed -n 's/.*=\([^ ]*\).*/\1/p' file
or, if you prefer:
sed 's/.*=\([^ ]*\).*/\1/p;d' file
Put the string you want to capture in a backreference:
sed 's/.*=\([^ =]*\) .*/\1/'
or do the substitution piecemeal;
sed -e 's/.*=//' -e 's/ .*//'
sed 's/[^=]*=\([^ ]*\) .*/\1/' inputfile
Match all the non-equal-sign characters and an equal sign. Capture a sequence of non-space characters. Match a space and the rest of the line. Substitute the captured string.
A chain of grep can do the trick.
grep -o '[=][a-zA-Z0-9]*' file | grep -o '[a-zA-Z0-9]*'