LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-23-2012, 12:30 PM   #1
Zimu
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Rep: Reputation: Disabled
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!
 
Old 10-23-2012, 12:50 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Try throwing in a pipe (|) between the 2 regexes and I would probably include -r switch so you don't have to escape it.
 
1 members found this post helpful.
Old 10-23-2012, 01:49 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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

Last edited by David the H.; 10-23-2012 at 01:56 PM.
 
1 members found this post helpful.
Old 10-23-2012, 03:20 PM   #4
Zimu
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
thank you guys!!i have solved my problem
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
insert tab at beginning of each line in file - and other sed questions robotrock Linux - Newbie 6 08-19-2015 01:31 AM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 11:48 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 10:14 PM
Line Replacement help needed. pinga123 Linux - Newbie 2 11-05-2009 03:46 AM
sed command to insert tab? iluvatar Linux - Software 4 06-29-2004 02:56 AM


All times are GMT -5. The time now is 03:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration