LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Append text with AWK or SED? (https://www.linuxquestions.org/questions/linux-newbie-8/append-text-with-awk-or-sed-796572/)

LostChild1 03-19-2010 04:11 PM

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!

schneidz 03-19-2010 04:45 PM

i would use sed to substitute.
whats the difference between a substitute and an append ?

syg00 03-19-2010 05:45 PM

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 ...

jschiwal 03-19-2010 06:05 PM

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.

LostChild1 03-19-2010 11:01 PM

Quote:

Originally Posted by schneidz (Post 3904970)
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.


syg00 03-19-2010 11:40 PM

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.

schneidz 03-20-2010 08:46 AM

Quote:

Originally Posted by LostChild1 (Post 3905182)
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.


All times are GMT -5. The time now is 12:09 AM.