Nawk Split


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Nawk Split
# 1  
Old 01-25-2006
Nawk Split

Hello. I have a input file in this format:
line1data :: line1data :: line1data
line2data :: line2data :: line2data

I would like to split each lines data element into an array:

array1[element1]=line1data
array1[element2]=line1data
array1[element3]=line1data

array2[element1]=line2data
array2[element2]=line2data
array2[element3]=line2data

I have been trying to do this with the nawk split function
Code:
#!/bin/bash
declare -a dataArray
dataArray=( `nawk -vline=$inputfileline '
BEGIN { 
  split(line,lineArray,"::")
  print { lineArray }
}'` )

However, this is not working. Any ideas?

Thank you
# 2  
Old 01-25-2006
hmm, I am not very clear on what you are trying to do
I tried the following and it is what probably u are looking for

set -a arrData ## this is for kshell, may be it is declare for bash
arrData=`awk ' {
split($0,arr,"::");
printf("%s\n", arr[1]);
printf("%s\n", arr[2]);
printf("%s\n", arr[3]);
}' yourfilename`

echo $arrData
line1data line1data line1data line2data line2data line2data

so works for me this way.
# 3  
Old 01-27-2006
Thanks Penguin. I did something like that. The input file will have comments embedded so first I get rid of all the junk and then dynamically create arrays and populate.

Code:
idx=1
while read entry
do
  if [ ${entry:0:1} == "#" ]; then
    continue
  else
    eval "set$idx=( `echo $entry | nawk ' { v=split($0,a,"::"); if ( v > 0 ) { print a[1] " " a[2] " " a[3] " " a[4]; } } '`)"
    idx=$(($idx+1))
    setIdx=$(($idx-1))
  fi
done < manifest

The problem I am having now is referencing the created arrays. When I try to dump the content like this I get a bad substitution error. Any ideas? Thanks.

Code:
a=1
while (( $a <= $setIdx ))
do
 echo "${set$a[@]}"
 ((a += 1))
done

# 4  
Old 01-29-2006
Use eval again...
Code:
a=1
while (( $a <= $setIdx ))
do
  eval "echo \${set$a[@]}"
  (( a += 1 ))
done
exit 0

Cheers
ZB
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split and Rename Split Files

Hello, I need to split a file by number of records and rename each split file with actual filename pre-pended with 3 digit split number. What I have tried is the below command with 2 digit numeric value split -l 3 -d abc.txt F (# Will Produce split Files as F00 F01 F02) How to produce... (19 Replies)
Discussion started by: techedipro
19 Replies

2. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

3. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

4. Shell Programming and Scripting

NAWK: changing string-format with split

Hi all, I try to make a awk-script, which counts lines, summarized by pdf and xml. So far it works, but for sorting reasons, I'd like to change the format from the field $1 from dd-mm-yyyy to yyyy-mm-dd. This works find, but: split() and sprintf() prints its output (for no reason, the results... (2 Replies)
Discussion started by: regisl67
2 Replies

5. Shell Programming and Scripting

help with nawk

hi guys, I am writing a code and have stuck at one point. Inside nawk I am storing my desired variable a, I just need to find if a is present in an external file error.log or not. If yes, print something. grep or for loop not working properly inside nawk. Sample code provided. nawk ' BEGIN... (5 Replies)
Discussion started by: shekhar2010us
5 Replies

6. Shell Programming and Scripting

using nawk

help out with code. two files aaa bbb contains some records..output file xyz should be like this..see below i/p file:aaa 08350|60521|0000|505|0000|1555|000|NYCMT|Pd_1 |-11878 i/p file: bbb 60521|60510 o/p file :xyz 60510|08350|60521|0000|505|0000|1555|000|NYCMT|Pd_1 |-11878 (5 Replies)
Discussion started by: Diddy
5 Replies

7. Shell Programming and Scripting

Nesting - two nawk into one nawk

hi people; this is my two awk code: nawk '/cell+-/{r=(NF==8) ? $4FS$5FS$6 : NF==7 ? $4FS$5 : $4 ;c=split(r,rr);for (i=1;i<=c;i++){if(rr != "111111"){printf($3" %d ""\n",(i+3))}}printf("")}' /home/gc_sw/str.txt > /home/gc_sw/predwn.txt nawk -F'*' '{gsub(/ *$/,"")}$0=$1$($NF-2)'... (2 Replies)
Discussion started by: gc_sw
2 Replies

8. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

9. UNIX for Dummies Questions & Answers

Split BIG report using nawk

I have the following nawk script: nawk -F: '{ if($0 ~ "^Report No") {fl=1; i=0;} if(fl==1){data=$0; i++} if($0 ~ "^BE NO:") { fname = "reprot_"$2".lis"; gsub(" ","",fname); for(j=0;j<i;j++) print data > fname; fl=0; } else if(fl==0) print $0 > fname; }' filename When I try to apply... (1 Reply)
Discussion started by: raychu65
1 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question