LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 03-19-2010, 05:11 PM   #1
LostChild1
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Rep: Reputation: 0
Append text with AWK or SED?


Hi there,

I'm doing an assignment for school, and while we have not yet learned regular expressions, my teacher would like us to use AWK to append text to certain lines in a file.

What we've had to do is creating a listing of all links and directories in the /etc folder and place them in a text file. From this, we were to cut the first field (Permissions) and the ninth field (filename) and create another text file.

Now the part that I'm struggling with is this and I'm not sure if I should be using sed or awk. We're supposed to ADD the text "DIR" to the beginning of any line that is a directory, and "LNK" to any that is a link, like so:

DIRdrwxr-xr-x redhat-lsb
DIRdrwxr-xr-x rhgb
LNKlrwxrwxrwx rnDIRc.key
DIRdrwxr-xr-x rpm

Obviously, I realize that the first character in the permissions denotes what sort of file it is, hence when I created my text file I used

ls -l | grep "^d" > file.txt
and
ls -l | grep "^l" >> file.txt

I'd like to learn how to properly do this, but struggling through pages on the internet hasn't been helping, nor has the --help command. Therefore, while I would like the answer, I'd also like to know what the heck I'm doing

For example, I know I could delete lines with regex using

d/[STUFF I WANT TO DELETE]/g (To get rid of all occurences)
and I can substitute using s/foo/bar

But I'm unaware of anyway to ADD text upon certain occurences, let alone two different circumstances.

Thank you!
 
Old 03-19-2010, 05:45 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,150

Rep: Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887
i would use sed to substitute.
whats the difference between a substitute and an append ?
 
Old 03-19-2010, 06:45 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,962

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Just to expand on that a little why not substitute "DIRd" in place of "d" ?. Using the appropriate anchor of course.
I too would use sed for this, but awk will do too - as will perl or python or ...
 
Old 03-19-2010, 07:05 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
A couple hints, since this is homework. In sed and awk, use /<pattern>/ to select lines you want to work with.
In sed, read the man page for that '&' means. In awk, look in the info or man page for $0. Also learn how to negate a match in awk.

Last edited by jschiwal; 03-19-2010 at 07:06 PM.
 
Old 03-20-2010, 12:01 AM   #5
LostChild1
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by schneidz View Post
i would use sed to substitute.
whats the difference between a substitute and an append ?
A substitute would replace the "d" with "DIR" leaving "DIRrwx..." instead of an append which would add the "DIR" before the "d" leaving "DIRdrwx..."

syg00, I followed your logic (I can't believe I didn't think of that. I was so focused on trying to append :P) and it worked like a charm. I used two seperate sed commands in order to substitute the appropriate text for each case (l and d)

jschiwal, I read the man page, and spotted the & symbol as you suggested but the explanation escaped me...

Code:
 s/regexp/replacement/
              Attempt to match regexp against the pattern space.  If successful, replace  that  portion  matched
              with replacement.  The replacement may contain the special character & to refer to that portion of
              the pattern space which matched, and the special escapes \1 through \9 to refer to the correspond-
              ing matching sub-expressions in the regexp.
 
Old 03-20-2010, 12:40 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,962

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
You can combine the two into one
Code:
sed -e '<first substitution>' -e '<next substitution>' ...
The ampersand is somewhat of a special case - a good tutorial will be handy. Search here (LQ), several are recommended regularly.
 
Old 03-20-2010, 09:46 AM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,150

Rep: Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887
Quote:
Originally Posted by LostChild1 View Post
A substitute would replace the "d" with "DIR" leaving "DIRrwx..." instead of an append which would add the "DIR" before the "d" leaving "DIRdrwx..."
good... since this was homework i was trying to hint at you of how to think about it but syg00 ended up giving you the answer anyways.
 
  


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
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 02:48 PM
translating text with awk or sed mek Programming 5 11-13-2009 08:57 AM
Text substitution and processing with sed and awk shanecraddock@gmail.com Linux - Newbie 1 12-18-2008 12:34 PM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 01:34 AM
sed script to append variable text gmartin Linux - General 4 12-27-2006 05:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:26 AM.

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