LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 08-05-2010, 06:11 PM   #1
robotrock
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
Question insert tab at beginning of each line in file - and other sed questions


Hello all,

Firstly, I've searched at length and can't seem to find the info so I decided to join this community and ask away. If this is a question that has been answered a billion times please point me in the right direction.

I am trying to create a shell script where a user can specify a file with a list of logins and the script will create a batch file with specific information in specific columns.

Example:

loginfile.txt has

User1
User2
User3

I need the output to be as below:

1 User1 Date 12/31/9999
2 User2 Date 12/31/9999
3 User3 Date 12/31/9999

Where the columns are separated as below.

1[tab][tab]User1[tab]date +%D[tab]12/31/9999
2[tab][tab]User2[tab]date +%D[tab]12/31/9999
3[tab][tab]User3[tab]date +%D[tab]12/31/9999

I can use the nl utility to get the numbers easily enough, but I need two tab separations between the number and the user list. Is there a sed command that will insert at the beginning of each line? If so I can just run nl after I get some tabs up in the front.

Any assistance is appreciated.
 
Old 08-05-2010, 06:19 PM   #2
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 66
Code:
sed -i 's/^/\t\t/' <filename>
 
Old 08-05-2010, 06:27 PM   #3
robotrock
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Original Poster
Rep: Reputation: 0
well that was simple!...I feel silly now.

Thanks very much Meson!
 
Old 08-13-2015, 10:08 AM   #4
Lexus45
Member
 
Registered: Jan 2010
Location: Kurgan, Russia
Distribution: Slackware, Ubuntu
Posts: 343
Blog Entries: 3

Rep: Reputation: 47
Hello.

I want to add a line to the file, and it must start with 2 tabs.

The original piece of text is this:
Code:
		Set(CDR(accountcode)=${CONTEXT});
		Set(NUMBER=${EXTEN});
		Dial(SIP/to-freeswitch/666);
		Hangup;
I'd like to get this:
Code:
		Set(CDR(accountcode)=${CONTEXT});
		Set(NUMBER=${EXTEN});
                Set(TIMEOUT(absolute)=1800);
		Dial(SIP/to-freeswitch/666);
		Hangup;

So, I do:
Code:
sed '/Dial/ i /\t\t\Set(TIMEOUT(absolute)=1800);' extensions.ael

And finally get not exactly what I want:
Code:
		Set(CDR(accountcode)=${CONTEXT});
		Set(NUMBER=${EXTEN});
/		Set(TIMEOUT(absolute)=1800);
		Dial(SIP/to-freeswitch/666);
		Hangup;
How can I get rid of '/' at the beginning of the line? Thank you.
 
Old 08-13-2015, 11:33 AM   #5
auge
Member
 
Registered: May 2002
Location: Germany
Distribution: CentOS, Fedora, LFS
Posts: 35

Rep: Reputation: Disabled
Here you go:
Quote:
sed 's/Dial/Set(TIMEOUT(absolute)=1800);\r\n\t\tDial/' extensions.ael
 
1 members found this post helpful.
Old 08-19-2015, 12:22 AM   #6
Lexus45
Member
 
Registered: Jan 2010
Location: Kurgan, Russia
Distribution: Slackware, Ubuntu
Posts: 343
Blog Entries: 3

Rep: Reputation: 47
One more question.

There are many lines like

Code:
AGI(${CIDCONVERT}/CONVERT,3823,${CALLERID(num)},Taxi-696 73823902290);
AGI(${CIDCONVERT}/CONVERT,3592,${CALLERID(num)},Office-557 35928888802,"BG");
and so on.

I'd like to add this to all lines having CIDCONVERT word before the last parenthesis:

Code:
,"RU"
To get this:
Code:
AGI(${CIDCONVERT}/CONVERT,3823,${CALLERID(num)},Taxi-696 73823902290,"RU");
AGI(${CIDCONVERT}/CONVERT,3592,${CALLERID(num)},Office-557 35928888802,"BG","RU");

Last edited by Lexus45; 08-19-2015 at 12:26 AM.
 
Old 08-19-2015, 12:31 AM   #7
Lexus45
Member
 
Registered: Jan 2010
Location: Kurgan, Russia
Distribution: Slackware, Ubuntu
Posts: 343
Blog Entries: 3

Rep: Reputation: 47
Oh... Seems that I've done it

Code:
sed '/CIDCONVERT/s/);/,"RU");/g' extensions.ael

ANd finally to remove just added everywhere ,"RU" if a line contains another country-code, for example ,"BG":

Code:
sed '/,"BG"/s/,"RU"//g' extensions_CIDCONVERT_OK.ael

Last edited by Lexus45; 08-19-2015 at 12:40 AM.
 
  


Reply

Tags
sed


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
sed insert # at the beginning of a line ilo Linux - Newbie 17 12-19-2012 08:21 AM
Insert a line in the test file with sed say_hi_ravi Programming 16 07-16-2010 10:24 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 09:14 PM
using sed to insert line into file and overwrite the current file jadeddog Programming 3 06-11-2009 07:14 PM
sed command to insert tab? iluvatar Linux - Software 4 06-29-2004 01:56 AM


All times are GMT -5. The time now is 04:30 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