INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Variable number of fields

Variable number of fields

(OP)
I have a list of files that should contain the following

Quote:


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

Quote:

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

Quote:

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

RE: Variable number of fields


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

RE: Variable number of fields


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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close