LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   awk and sorting (http://www.linuxquestions.org/questions/linux-software-2/awk-and-sorting-822178/)

BerzinTehvs 07-26-2010 05:13 AM

awk and sorting
 
Hi!

I have in bash script the following line:
Code:

TMP1=`echo $TMPO | awk '{FS=", "} {for (i = 1; i <= NF; i++) print $i}' | sort -z`
which sorts words within string, echo $TMP1 shows that everything is OK, but when i try to :
Code:

Output=`echo $line | awk -F $DELIM  '{OFS=FS; $1="'"$TMPW"'"; $4="'"$TMP1"'" } {print}'`
I get:
awk: {OFS=FS; $1="word1"; $4="word2,
awk:_________________________^ unterminated string

(underline put by me for positioning)
Whats wrong with TMP1? If i manipulate TMP1 and insert into Output without sorting, everything works.

ghostdog74 07-26-2010 05:23 AM

use the $() syntax, and when declaring FS, do it inside BEGIN{} block, or use -F. Quote your variables. Use \047 as the single quote in awk.

Code:

TMP1=$(echo $TMPO | awk 'BEGIN{FS=", "}{for (i = 1; i <= NF; i++) print $i}' | sort -z)
Output=$(echo "$line" | awk -F"$DELIM" '{ $1="\047"$TMPW"\047"; $4="\047"$TMP1"\047" } {print}' OFS="$DELIM")


BerzinTehvs 07-26-2010 05:46 AM

Quote:

Originally Posted by ghostdog74 (Post 4045486)
use the $() syntax, and when declaring FS, do it inside BEGIN{} block, or use -F. Quote your variables. Use \047 as the single quote in awk.

Code:

TMP1=$(echo $TMPO | awk 'BEGIN{FS=", "}{for (i = 1; i <= NF; i++) print $i}' | sort -z)
Output=$(echo "$line" | awk -F"$DELIM" '{ $1="\047"$TMPW"\047"; $4="\047"$TMP1"\047" } {print}' OFS="$DELIM")


the code doesnt' give whats expected:

1. in TMP1 all delimiters are lost, wordsa are separated by spaces, not original ", ", which was in my example;
2. Output somehow becomes trippled original string represented by $line.

I'd be happy understanding why TMP1 acquired in the original way gives that error. (actually, if I use the TMP1 acquired in the way advised for insertion into Output in original way, I get the same error, so I assume everything hides within Output code line)

BerzinTehvs 07-26-2010 09:22 AM

Hi,
I am now so far:
Code:


TMP1=`echo $TMPO | awk -F ", " ' {RS=", "; n=split($0,arr); asort(arr); for (i = 1; i <= n; i++) print arr[i]}'`

the only problem is that I am not getting words separated by ", ".

if i modify it like this:
Code:


TMP1=`echo $TMPO | awk -F ", " ' {RS=", "; n=split($0,arr); asort(arr); for (i = 1; i <= n; i++) print arr[i]", "}'`

I get one unnecessary separator at the end.

How could I improve it? Adding ORS to the script gives the same extra separator at the end.

GrapefruiTgirl 07-26-2010 09:30 AM

EDIT - Sorry, I had a typo - back in a minute :)

GrapefruiTgirl 07-26-2010 09:36 AM

Code:

echo "hello, goodbye, rotten, apple, core" | \
awk -F ", " ' {RS=", "; n=split($0,arr); asort(arr); for (i = 1; i < n; ++i) print arr[i]", "; print arr[i]}'

There - sorry about that. :)

BerzinTehvs 07-26-2010 12:43 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 4045712)
Code:

echo "hello, goodbye, rotten, apple, core" | \
awk -F ", " ' {RS=", "; n=split($0,arr); asort(arr); for (i = 1; i < n; ++i) print arr[i]", "; print arr[i]}'


while the example runs smoothly, in my script the resulting string anyway gets an extra comma at the end (and without ORS it crashes next awk with message about unterminated string)

so, I killed the last comma with
Code:

TMP2=`echo $TMP1 | sed '$s/.$//'`

GrapefruiTgirl 07-26-2010 12:46 PM

Could you show a sample of exactly what the input file looks like (what it contains) as well as the exact output you desire? I'll have another look at it :)

BerzinTehvs 07-31-2010 09:44 AM

see this thread:
http://www.linuxquestions.org/questi...locale-822982/


All times are GMT -5. The time now is 06:50 PM.