Need help with AWK code using xargs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with AWK code using xargs
# 1  
Old 05-29-2012
Need help with AWK code using xargs

Hi,

I have a colon-separated file which contains names of users, among other details. My aim is to extract the line with the name and assign the name to a variable. A sample file is as follows --
Code:
ID:		123456
DEPARTMENT:	xyz
NAME:		Bar, Foo

Considering the Tabs between the colon and the next character, I use the following code to extract the name --
Code:
nameVar=`grep -w ^"NAME:" inputFile.txt | awk -F" " '{ for ( i = 2; i++; i <= NF) {print $i} }' | xargs`

This code works fine, unless the name contains a single-quote (say, O'Bar, Foo). In such a case, xargs complains that a quote is missing, and hence, the nameVar remains blank. I tried to overcome it, using the following code --
Code:
nameVar=`grep -w ^"NAME:" inputFile.txt | awk -F" " '{ for ( i = 2; i++; i <= NF) { if ( $i = *\'* ) { i=\"$i\" } { print $i } }' | xargs`

This returns a syntax error message. I'm using a standard KSH shell on a Solaris box.

Could anyone please guide me to a solution?

Thanks,
Subu
# 2  
Old 05-29-2012
Hi Subu1987,

One way:
Code:
$ cat infile
ID:             123456
DEPARTMENT:     xyz
NAME:           Bar, Foo
$ awk 'BEGIN { FS = ":" } $1 ~ /^NAME$/ { sub( /[[:space:]]+/, "", $2 ); print $2 }' infile
Bar, Foo

# 3  
Old 05-29-2012
Quote:
Originally Posted by Subu1987
... My aim is to extract the line with the name and assign the name to a variable.
Is this the right solution?

Code:
awk 'BEGIN{FS=":"}$1 ~ /Name/ {print $2}' infile >outfile

# 4  
Old 05-31-2012
Hi birei and sdf,

Thank you for your contributions. Unfortunately, these did not resolve my problem. Smilie Perhaps I was not able to explain my requirement correctly. Stripping the name was not an issue for me. The issue was names with single-quotes in them, like O'Bar, Foo. In such a case, the single-qoute in the name would be paired with the single-qoute at the beginning of the awk program, causing rest of the awk program to be ignored, and hence, a syntax-error. So, the single-qoute in the name had to be escaped, before it was passed on to the awk program. I achieved this by using the following code --
Code:
nameVar=`grep -w ^"NAME:" inputFile.txt | sed -e "s/\'/\\\'/g" | awk -F" " '{ for ( i = 2; i++; i <= NF) {print $i} }' | xargs`

# 5  
Old 05-31-2012
Adding suffix in row

I have an input file like
RELTEST_SITE2||RELTEST_SITE2|RELTEST_SITE2|Huawei-BTS|Cell Sites|Huawei|||South/KERALA/COCHIN/KAZIKODE|Active|Not Maint|12|fiber|GCRNUJRNUJB001HBT022|357||RELTEST_BSC2|||||

and i want to add suffix to GCRNUJRNUJB001HBT022 as "12345"

Kindly suggest a query.

Also suggest a query which works for 5 10 rows
# 6  
Old 05-31-2012
Hello,

Please do not ask new questions in existing threads. Unless you have new information to a particular problem, please create a new thread in the appropriate forum.

Also, please search the forums first, as it might be that someone has already posted an answer for a similar problem.

And please use code tags..

Best regards,
The UNIX and Linux Forums
# 7  
Old 05-31-2012
Please open a new thread

Image
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Xargs, awk, match, if greater - as a one-liner

Hi I have multiple files for which I want to use awk for the following: Read each line in each file- if any of the columns match "PVALUE=" followed by the number, then print the line in case the number following "PVALUE=" is greater than 0.05. I did the following: ls *.txt | xargs -I @ -P15... (14 Replies)
Discussion started by: ts89490
14 Replies

2. UNIX for Dummies Questions & Answers

Xargs

Hi, can anyone tell me in detail ? what the following do in detail ? I am trying to get a largest number in a list Thanks Tao LARGEST=$(echo $* | xargs -n1 | sort -nr | tail -1) (3 Replies)
Discussion started by: ccp
3 Replies

3. Shell Programming and Scripting

Help with find, xargs and awk

Hi, I want to find some files and then search for some lines in it with a particular pattern and then write those lines into a file. To do this I am using something like this from command prompt directly. cd /mdat/BVG find -name "stmt.*cl" -newer temp.txt | xargs -i awk '/BVG-/{print}' {} >... (7 Replies)
Discussion started by: Sandhya Harsh
7 Replies

4. Shell Programming and Scripting

Help with xargs

hi Could any one please tell me the option using which we can run multiple commands using xargs I have list of files, I want to run dos2unix and chmod at one shot on them I tried google n searched man pages but couldnt really find the solution , please help right now im doing this ls... (4 Replies)
Discussion started by: sunilmenhdiratt
4 Replies

5. Shell Programming and Scripting

Xargs and

Hello there, Let me show you a simple example of what I am trying to achieve: 1) I have an input text file with some lines: 1 a 2 b 3 c 2) And I want to run a command with these lines as arguments (+ arbitrary extra arguments). For example: $ command "1 a" "2 b" "3 c" "bye" I... (7 Replies)
Discussion started by: tokland
7 Replies

6. Shell Programming and Scripting

Help in using xargs

Hi, I have a requirement to RCP the files from remote server to local server. Also the RCP has to run in parallel. However using 'xargs' retrives 2 file names during each loop. How do we restrict to only one file name using xargs and loop till remaining files. I use the below code for... (2 Replies)
Discussion started by: senthil3d
2 Replies

7. Shell Programming and Scripting

Using xargs

hi i just want to know that how do we use xargs command to find files which are greater than specified memory in a given directory (6 Replies)
Discussion started by: sumit the cool
6 Replies

8. UNIX for Advanced & Expert Users

xargs -P

I discovered that GNU's xargs has a -P option to allow its processes to run in parallel. Great! Is this a GNU thing, or is it supported by other platforms as well? (4 Replies)
Discussion started by: otheus
4 Replies

9. Shell Programming and Scripting

why we use xargs..

hi , can anyone help me by saying why we use xargs.. is it acing like a place holder..? thanks, Krips. (3 Replies)
Discussion started by: kripssmart
3 Replies

10. UNIX for Dummies Questions & Answers

xargs

Can I use xargs to send a list of commands to a process, to be acted upon individually? Here's what I have: a file that contains numbers, one per line. The desired outcome it to send each number to a DB2 query. I thought xargs would work, but it doesn't. I tried it like this: cat file | xargs |... (4 Replies)
Discussion started by: jpprial
4 Replies
Login or Register to Ask a Question