Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable number of fields 1

Status
Not open for further replies.

brittany1

Technical User
Aug 17, 2007
12
US
I have a list of files that should contain the following
35001
35002
35003
35004
35005
35006

I have found many that do not have some of this content. Could be missing any one or any number of entrys shown above.

Code:
cat *.txt | while read file
do
grep 3500[1-6] file | tr '\012' ','
done

The code above will in example provide problem output - columns arent lined up

one.txt 35001,35002,35005,35006
two.txt 35002,35003,35004,35005,35006
three.txt 35001,35002, 35003,35004,35005,35006
four.txt 35001,35006

what I after is
one.txt 35001,35002,,,35005,35006
two.txt ,35002,35003,35004,35005,35006
three.txt 35001,35002, 35003,35004,35005,35006
four.txt 35001,,,,,35006

This way, when I move it into the Microsoft environment the colums will be aligned correctly.
Thanks in advance

 

Try this:
Code:
for file in *.txt
do
  echo  "$file: \c"
  awk '$1 ~ /3500[1-6]/{x=int(substr($1,length($1))); n[x]=$1}
  END{c="";for (x=1;x<=6;x++) {printf"%s%s",c,n[x]; c=","}; print}' $file
done

one.txt: 35001,35002,,,35005,35006
two.txt: ,35002,35003,35004,35005,35006
three.txt: 35001,35002,35003,35004,35005,35006
four.txt: 35001,,,,,35006
[3eyes]




----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

PS: The "$1 ~ " can be omitted:
Code:
for file in *.txt
do
  echo  "$file: \c"
  awk '/3500[1-6]/{x=int(substr($1,length($1))); n[x]=$1}
    END{c="";for (x=1;x<=6;x++) {printf"%s%s",c,n[x]; c=","}; print}' $file
done



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top