LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-31-2007, 09:37 PM   #1
ipguru99
LQ Newbie
 
Registered: Dec 2007
Posts: 2

Rep: Reputation: 0
Bash substitution -- Fairly beginner stuff!


Hello all,

Doing very tiny bash scripts, I seem to get down ok. But for some reason, the 'for x in `grep something... | cut .. ' stuff just doesn't stick. I have looked all over at samples and I always learn something new about sed or awk, or something new about piping stuff, but never seem to actually get it... so I thought I would post an example here of something I was trying to do the other day and see what people think. Thanks in advance!

I have a file with 200 names in it. I have manipulated it enough that I have almost all the fields filled with data. Either by using cut from another file or continuing to sed the thing to death, one field at a time. All of the fields are the same for all 200 users, so this works. My problem comes in where I need to pull a field (below, number 2.. the emailid) for all 200 lines and place it as the value for the 6th field (the quotes after displayname).
Code:
ca user1@email.com  '12345' default displayName  '' givenName xxx sn yyyy
(CreateAccount emailid 'password' default-class displayName-goes-between-the quotes... I am not concerned with anything after displayname yet)

So as an example, the script runs through the file, it pulls user1@email.com and I get a line that looks like this
Code:
ca user1@email.com  '12345' default displayName  'user1@email.com' givenName xxx sn yyyy
I hope I am explaining this correctly. I am sure there are a ton of ways to do it, and if they are easier, please let me know. I have messed around with
Code:
for x in (cat file1.txt | grep *???@*); do sed 's/\'\'/$x/g'
(I am doing this quickly and it was a couple of days ago.. so I am sure my regex stuff is wrong ;-), but at the time it was right... I just kept getting results like the same line 200 times, for every person).

Again, I am sure there are a couple of ways to do stuff like this... Thanks!
 
Old 12-31-2007, 09:46 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ipguru99 View Post
Code:
for x in (cat file1.txt | grep *???@*); do sed 's/\'\'/$x/g'
the for loop is redundant and is not the best way to do things, at least for your case.
If you want, provide a sample input files and show your desired output.
 
Old 01-01-2008, 04:43 AM   #3
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Is this what you want?
Code:
$ awk -v q="'" '{print $1,$3,$4,$5,q$2q,$7,$8,$9,$10}' file
ca '12345' default displayName 'user1@email.com' givenName xxx sn yyyy
if you don't mind the extra space (field 2 will turn into a space) then.
Code:
$ awk -v q="'" '{$6=q$2q;$2=""}1' file
ca  '12345' default displayName 'user1@email.com' givenName xxx sn yyyy

Last edited by angrybanana; 01-01-2008 at 05:14 AM. Reason: used -v to make code cleaner
 
Old 01-01-2008, 08:19 AM   #4
ipguru99
LQ Newbie
 
Registered: Dec 2007
Posts: 2

Original Poster
Rep: Reputation: 0
angrybanana, thanks! Once again, something new I learned about awk! Is the 'q' used because of the single quotes.. or can q be just a place holder for anything?

Ghostdog74, the first code snippet is the sample input file. The second code snippet is the desired result. In real life, the first one has about 200 lines, not 1. Each line of the file is almost identical, just the second field changes (user1@email.com, userA@email.com...) and that is the one I want to insert into the 5th field.

Thanks guys!
 
Old 01-01-2008, 04:31 PM   #5
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by ipguru99 View Post
angrybanana, thanks! Once again, something new I learned about awk! Is the 'q' used because of the single quotes.. or can q be just a place holder for anything?
-v variable = data

The reason I set a variable to single quote is to make the code more readable. Had I not done that, it'd be something like this.
Code:
$ awk '{print $1,$3,$4,$5,"'\''"$2"'\''",$7,$8,$9,$10}' file
 
  


Reply



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
Bash Command Substitution dakensta Programming 5 11-30-2006 03:10 PM
Varible substitution in Bash script RonV Linux - Newbie 2 06-04-2006 12:11 PM
Bash variable substitution daYz Programming 3 04-14-2006 01:16 PM
Bash Process Substitution joshholt Programming 4 10-11-2005 03:15 AM
Fairly simple bash problem xxx_Birdman_xxx Programming 5 06-16-2004 11:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:35 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
Open Source Consulting | Domain Registration