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

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

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

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

Hi Subu1987,

One way:

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

Is this the right solution?

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

Hi birei and sdf,

Thank you for your contributions. Unfortunately, these did not resolve my problem. :frowning: 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 --

nameVar=`grep -w ^"NAME:" inputFile.txt | sed -e "s/\'/\\\'/g" | awk -F" " '{ for ( i = 2; i++; i <= NF) {print $i} }' | xargs`

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

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

Please open a new thread