LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to insert a tab at beginnning of a line AND do some replacement? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-insert-a-tab-at-beginnning-of-a-line-and-do-some-replacement-4175433696/)

Zimu 10-23-2012 11:30 AM

How to insert a tab at beginnning of a line AND do some replacement?
 
Hey
I have tried to replace some symbols with tab, using sed command:

Code:

sed 's/[\t;, ]\{1,\}/\t/g' file
And insert a tab at beginning of each line :

Code:

sed 's/^/\t/g' file
But i don't know how to do both operations at the same time:(

Can anybody help me with this?

THX!

grail 10-23-2012 11:50 AM

Try throwing in a pipe (|) between the 2 regexes and I would probably include -r switch so you don't have to escape it.

David the H. 10-23-2012 12:49 PM

Actually, you have three options.

1) Use multiple expressions with the -e option.
Code:

sed -r -e 's/[\t;, ]+/\t/g' -e 's/^/\t/g' file
2) Concatenate expressions with semicolons (a kind of shortcut syntax for the above).

Code:

sed -r 's/[\t;, ]+/\t/g ; s/^/\t/g' file
3) Use regex grouping to specify optional strings to substitute (which is what grail was hinting at).

Code:

sed -r 's/(^|[\t;, ]+)/\t/g' file
The regex "+" character is equal to "{1,}", BTW.

Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Zimu 10-23-2012 02:20 PM

thank you guys!!i have solved my problem :D


All times are GMT -5. The time now is 06:38 PM.