A way: when the line starts with .set, grab the second and last field. When the .set line is found any line from then on grab the last field if the separator is an equal sign:
#!/bin/ksh
s_set=0 recset="" # get rid of the # double quotes tr -d '\"' < myfile | while read line do case "$line" in .set*) if [[ -n recset ]] then # get rid of the last pipe recset=$(echo "$recset" |sed 's/|$//g') echo $recset fi recset=$(echo "$line"|awk ' BEGIN { FS=":" } { print $2"|"$NF"|" } ') s_set=1 ;;
*) if [[ s_set -eq 1 ]] then nl=$(echo "$line"|awk ' BEGIN { FS="=" } { print $NF"|" } ') recset=${recset}${nl} fi ;; esac
done if [[ -n recset ]] then recset=$(echo "$recset" |sed 's/|$//g') echo $recset fi
awk 'BEGIN {FS = "=";c=0; } NR <=5 { next} /\.set/{ c=c+1 ;next} { array[c] = array[c]$2"|"} END { for (e in array) { b = gensub(/(\")|(\|$)/,"","g",array[e]) n = split(b,f,"|") printf("%s|%s|" , f[2] , f[3])
My file size is approximately about 6823009 Bytes. It is taking such a long time to complete if i run using your script. Please suggest another way to improvise this.
if you want to skip lines beginning with ..end, you can use
... /^\.\.end/ { next } ...
explanation: FS is field separator. I set it '=' , so that the first field $1 will be BSC_NAME, CELL_NAME etc and second field $2 will "BSC2","ANTAM16" and so on. I set c=0, so that i can store to array. come to that later. NR <=5 { next} : If skip reading the first 5 lines. /\.set/{ c=c+1 ;next} : when awk sees the line with .set, i increment c, so c = 1. and awk read the next record { array[c] = array[c]$2"|"} : this means store into array, array[c] will be "" (null) in the first run. since c is 1, then the value of array[1] will be concatenated with $2 (field 2) and then a "|". the result is a line with all $2 concatenated. END {: means before awk exits after processing every line, it performs the code inside END {} for (e in array) {: going through the array gsub(/(\")|(\|$)/,"",array[e]) : substitue every double quotes or the pipe at the end of the line with "" printf("%s|\n", array[e]) : after substitute, print the results.
finally, the final code can be like this: [code] awk 'BEGIN {FS = "=";c=0; } NR <=5 { next} /^\.\.end/ { next } /\.set/{ c=c+1 ;next} { array[c] = array[c]$2"|"} END { for (e in array) { gsub(/(\")|(\|$)/,"",array[e]) printf("%s|\n", array[e]) }
}' "file"
[/code]
As for awk reference, you can google for GNU awk. Then look thru the tutorial at its website. happy awking
With reference to previous: /\.set/{ c=c+1 ;next} { array[c] = array[c]$2"|"}
this says the items are stored into array, with indices denoted by the value of c, which are just numbers starting from 1 to whatver. so basically when you want to call the array values, it is like this: array[1], array[2].... however, when we displayed the array elements using this format of the for loop: for (e in array) { }, it list out the items in arbitary order. If you want ordered, then we have to go by indices. fortunately we have the array indices stored as numbers starting from 1, so its easy to call them out using this for loop format: for (e=1;e<=c;e++) { }
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE