Calling all the awk gurus out there.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Calling all the awk gurus out there.
# 1  
Old 03-18-2013
Calling all the awk gurus out there.

Hi all, I just signed up to the forums, although, I have lurked on here for awhile. Anyways, my issue is I am trying to get awk to spit out something I can use without having to spend hours in excel hell haha. So, I used sed to replace the spaces with semicolons and redirected that to a file.


This is what I intend to do:
Code:
User1 id=11111 gecos=John Doe groups=group1,group2 account_locked=false

But, because there is a space between the name in the gecos column, it messes everything up. So I need some way to combine the columns starting with gecos=, ignore the space, and end with a character a-z/A-Z and not get to the groups= column. I cannot for the life of me think how to translate this to a command. So, is there any easy way to go about this? It doesn't have to be awk, I just need something that will work. Thanks in advance.

Last edited by savigabi; 03-18-2013 at 09:55 PM.. Reason: code tags
# 2  
Old 03-18-2013
How about sed:
Code:
$ sed -e :a -e 's/\(.*\) \([a-z_]\{1,\}=\)/\1;\2/;ta' file
User1;id=11111;gecos=John Doe;groups=group1, group2;account_locked=false

Or just the values with awk:
Code:
$ awk -F' *[a-z_]+=' '{$1=$1}1' OFS=';'  file
User1;11111;John Doe;group1, group2;false

# 3  
Old 03-19-2013
Quote:
Originally Posted by Scrutinizer
How about sed:
Code:
$ sed -e :a -e 's/\(.*\) \([a-z_]\{1,\}=\)/\1;\2/;ta' file
User1;id=11111;gecos=John Doe;groups=group1, group2;account_locked=false

Or just the values with awk:
Code:
$ awk -F' *[a-z_]+=' '{$1=$1}1' OFS=';'  file
User1;11111;John Doe;group1, group2;false

Awesome, thank you so much Scrutinizer, that works perfectly! I will now know next time how to do this.
# 4  
Old 03-20-2013
Code:
$ echo "User1 id=11111 gecos=John Doe groups=group1,group2 account_locked=false" | sed "s/ \([^= ]\{1,\}=\)/;\1/g"
User1;id=11111;gecos=John Doe;groups=group1,group2;account_locked=false

This User Gave Thanks to anbu23 For This Post:
# 5  
Old 03-20-2013
@anbu, you are right it could also be done from left to right instead of right to left (in fact the = "anchor" provides mini-right to lefts) The [^=] might not even be required and an asterisk gets rid of more than one space:
Code:
sed 's/ *\([^ ]\{1,\}=\)/;\1/g'

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-20-2013
Just another one.
Code:
sed 's/[^ ]*=/;&/g;s/ ;/;/g' yourfile

or to deal with the "more than one space" issue :
Code:
sed 's/[^ ]*=/;&/g;s/ *;/;/g' yourfile


Last edited by ctsgnb; 03-20-2013 at 06:38 AM..
# 7  
Old 03-20-2013
All excellent information, thanks all. This has saved me so much time.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling array inside awk

Hello I have the file df.tmp FS is actually the / FS but escape character\ and end of line $ is used in order to fetch exctly / and not other filesystems. awk '/\/$/ {print $(NF-1)+0}' df.tmp will work properly and return a value eg. 60 but when I am trying to issue the command with the array... (3 Replies)
Discussion started by: drbiloukos
3 Replies

2. Shell Programming and Scripting

calling awk from php not working

I want to run awk from php to do some text processing. I am giving an extremely simple example below: onecol.awk file ------------------- { print "Hello!"; } f1.txt --------- aaa ccc eee f2.txt --------- (6 Replies)
Discussion started by: mary271
6 Replies

3. Shell Programming and Scripting

Calling awk fom csh

I have to call two awk scripts where the second one used the output from the first one. Am wondering if it may happen that the second awk might start before the first awk finished creating the file... if ($nAnomaly == 1) then awk -v anomaly=$Anom -v zloc="$zmin/$zmax" -v dz=$dz \ ... (1 Reply)
Discussion started by: kristinu
1 Replies

4. Shell Programming and Scripting

Calling awk from csh

I am trying to call awk from a csh script using awk '{print $1, -$2, $3}' $fvmod.vel > $fvmod.xzv and getting awk: Command not found. Running awk '{print $1, -$2, $3}' $fvmod.vel > $fvmod.xzv on the command line with the actual filenames works (2 Replies)
Discussion started by: kristinu
2 Replies

5. Shell Programming and Scripting

Calling of search using awk

Database rina lives:fatin:20:20:20 rina:fatin:20:20:20 i am having a small problem in extracting out the information from the database. For example, if i were to input a book titled rina into my search , i would only want it to display the row which has the title rina, like this ... (4 Replies)
Discussion started by: gregarion
4 Replies

6. Shell Programming and Scripting

calling a method inside awk

Hi All, How do we call a method existing in another file inside awk. After matching a pattern i want to call a method secureCopy that exists in another file, but method not getting called: ls -l | awk -v var2=$servername -v var1=$srcserverpath -v var3=$tgtpath '... (1 Reply)
Discussion started by: abhinav192
1 Replies

7. Shell Programming and Scripting

calling mysql gurus : need help with my view

hi there I have a view which is working fine, but i have been told that i need to make sure the resulting output excludes all rows with nic_status equal to the string "removed". Does anybody know in which part of the code below i would place the conditional ... WHERE nic_status !=... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

8. Solaris

calling script from awk

Hi, I'm not sure, there is solution exist for this problem. I'm facing problem in calling a unix script from awk code. Here is code nawk -F'=' ' BEGIN { } function runtest(string) { LINE } /^/ { # getting module name t=$1 module=substr(t,2,(length(t)-2)) runtest(module) }... (1 Reply)
Discussion started by: luckybalaji
1 Replies

9. Shell Programming and Scripting

Calling ipcrm from awk

Hello, With next simple command line I get all allocated semaphore numbers: ipcs -s | awk 'NR > 3 {print $2}' Now I want to add ipcrm -s before each semaphore id and actually delete every last of them. The question is: Can I do the operation in a command itself, without redirecting awk... (2 Replies)
Discussion started by: BaruchLi
2 Replies

10. Shell Programming and Scripting

Help with shell script - Unix Gurus calling

Unix Gurus, I have been breaking my head to get this done..seems simple.. I need to read a flat file and based on a key word in a line, i need to skip the previous 3 lines. eg : Line1 Line2 Line3 Line4 Line5 Line6 Error Line7 Line8 Line9 Error Line10 (4 Replies)
Discussion started by: ravred
4 Replies
Login or Register to Ask a Question