how can I replace a tab or empty spaces with a single semicolon?
This is what I'm trying:
sed -i.bak 's/(\s)+|(\t)+/,/g' aws_lab.txt
+/ was unexpected at this time.
Thanks
Just use the appropriate POSIX character class:
sed 's/[[:space:]]\{1,\}/,/g' aws_lab.txt
Very few seds will recognize the \s shorthand for [[:space:]] and since a tab is one of the characters included in [:space:] you dont need to specify it separately. Also, + is an ERE metacharacter, not BRE as supported by sed by default so you'd need to add the -E arg to sed to use it (only supported by GNU and OSX sed variants).
This might work for you (GNU sed):
sed -i.bak 's/[ \t][ \t]*/:/' file
Or
sed -ri.bak 's/[ \t]+/:/' file
Where there is an explicit space infront of the tab:
sed -i.bak 's/[ \t][ \t]*/:/g' file
Will replace repeated patterns throughout a line.
Related
I have this content in a file where I want to replace spaces at certain positions with pipe symbol (|). I used sed for this, but it is replacing all the spaces in the string. But I don't want to replace the space for the 3rd and 4th string.
How to achieve this?
Input:
test test test test
My attempt:
sed -e 's/ /|/g file.txt
Expected Output:
test|test|test test
Actual Output:
test|test|test|test
sed 's/ /\
/3;y/\n / |/'
As newline cannot appear in a sed pattern space, you can change the third space to a newline, then change all newlines and spaces to spaces and pipes.
GNU sed can use \n in the replacement text:
sed 's/ /\n/3;y/\n / |/'
If the original input doesn't contain any pipe characters, you can do
sed -e 's/ /|/g' -e 's/|/ /3' file
to retain the third white space. Otherwise see other answers.
You could replace the 'first space' twice, e.g.
sed -e 's/ /|/' -e 's/ /|/' file.txt
Or, if you want to specify the positions (e.g. the 2nd and 1st spaces):
sed -e 's/ /|/2' -e 's/ /|/1' file.txt
Using GNU sed to replace the first and second one or more whitespace chunks:
sed -i -E 's/\s+/|/;s/\s+/|/' file
See the online demo.
Details
-i - inline replacements on
-E - POSIX ERE syntax enabled
s/\s+/|/ - replaces the first one or more whitespace chars
; - and then
s/\s+/|/ the second one or more whitespace chars on each line (if present).
Keep it simple and use awk, e.g. using any awk in any shell on every Unix box no matter what other characters your input contains:
$ awk '{for (i=1;i<NF;i++) sub(/ /,"|")} 1' file
test|test|test test
The above replaces all but the last " " on each line. If you want to replace a specific number, e.g. 2, then just change NF to 2.
I have a sed oneliner which removes excess whitespace:
sed -e 's/^\s*//' -e 's/\s*$//' -e 's/\s{2,}/ /g'
When I test it on " \tone1 two\t3three\t ", the sed removes the whitespace at the beginning and end of the line but doesn't match the excess whitespace between words, and sed returns \tone1 two\t3three. What I want is \tone1 two 3three, so sed -e 's/[ \t]{2,}/ /g' is not functioning.
regexr.com shows the expression as functional.
My version is GNU sed version 4.2.1.
{ and } need to be escaped in basic regex mode that sed uses.
However, you can use this sed with a single substitution with alternation:
sed -E 's/^[[:blank:]]+|[[:blank:]]+$|[[:blank:]]{2,}//g' file
POSIX character class [[:blank:]] matches a space or tab characters.
I am trying to use sed to replace strings with special characters in a text file. The sed command is becoming too much complicated. If someone could please help me with the exact command.
Code -
sed -i 's;PS1='${HOSTNAME} [$ORACLE_SID] $PWD> ';PS1="${COL_YELLOW}'CUSTOMER TEST:${HOSTNAME}:[$ORACLE_SID]:$PWD> '${COL_END}";g'
I tried to escape the special characters as below but its not working.
sed -i 's;PS1=\'\${HOSTNAME} [\$ORACLE_SID] \$PWD> \';PS1="\${COL_YELLOW}\'CUSTOMER TEST:\${HOSTNAME}:[\$ORACLE_SID]:\$PWD> \'\${COL_END}";g' .bash_profile_backup
This might work for you (GNU sed):
sed -i 's|PS1='\''${HOSTNAME} \[$ORACLE_SID\] $PWD> '\''|PS1="${COL_YELLOW}'\''CUSTOMER TEST:${HOSTNAME}:[$ORACLE_SID]:$PWD> '\''${COL_END}"|g' file
N.B. ' need to be quoted in both the pattern and replacement whereas [] needs to be escaped in the pattern only.
How can I replace ; with ;\n (semicolon followed by a newline) in sed?
I've tried building off of
sed s/;/\\n/g file
and
sed -e '/;/G' file
but I can't get either to work
You need to cheat a bit: in bash you can say
sed $'s/;/;\\\n/g'
or, portably (POSIX):
sed "s/;/;$(printf '\\\n')/g"
sed does not portably/reliably handle backslash-escapes anywhere but in the pattern, and even there it's limited (POSIX only requires that \n be handled, not \t or the others). Note that you also need a backslash before the \n so sed doesn't read it as the end of the command.
sed -ie 's/;/;\n/g' <file>
That's assuming you want to do it inline in the file, remove the "i" and just use "-e" if that's not the case.
I would like one sed command to accomplish the following:
$ sed s'/:/ /g' <and> sed s'/=/ /g'
That is, I would like to write
sed s'/<something>/ /g'
and have both = and : replaced by space.
sed s'/[:=]/ /g'
Brackets mean "any one of".
One option is also to use sed -e, like this. Although you don't need it in this case, it's however a good option to know about.
sed -e 's/:/ /' -e 's/..../ /' file
Sanjay's answer solves it. Another option that works with only one sed command is to separate each s substitution with a semicolon
sed 's/:/ /g ; s/=/ /g' file
or in separate lines in a script
sed 's/:/ /g
s/=/ /g' file
Those may be handy in other situations.