Link to home
Start Free TrialLog in
Avatar of bje
bjeFlag for United States of America

asked on

pull a value from a tab delimited file

Hello,
I  am working on pulling a value from a tab delimited file.  Need to pull the value from the 8th tab position of a file, so it can be used as the file name for the file.

This is what I have,

FILELIST=`ls -1 2> /dev/null`
          for file in $FILELIST
            do
              case $file in      
                    WSC_862*.tsv ) newfilename = '$file | cut -d c -f8'
                                mv $file $destination_dir/newfilename.$DateTimeStamp;;
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Doesn't look too bad, except for some mistakes.

Which line of the files do you want to pull the value from?
If there is only one line in each file this could work:

          for file in $FILELIST
            do
              case $file in      
                    WSC_862*.tsv ) newfilename=$(awk '{print $8}' $file)
                                mv $file $destination_dir/$newfilename.$DateTimeStamp;;
              esac
             done

If there is more than one line in the inputfile(s) you should have a unique criterion to select the appropriate line:

          for file in $FILELIST
            do
              case $file in      
                    WSC_862*.tsv ) newfilename=$(awk '/unique_search_string/ {print $8}' $file)
                                mv $file $destination_dir/$newfilename.$DateTimeStamp;;
              esac
             done


wmp
Avatar of bje

ASKER

Thank you
There are multiple lines in the files.  Would need to look at the 4th line of the file and the values change.  Can you Identified the 4th line without a unique value?
Yes.

newfilename=$(awk 'NR==4 {print $8}' $file)

"NR" is an automatic counter for "Number of Records" in awk.
Avatar of bje

ASKER

Thanks.  This is working well, just one more question.

Does the print $8 count the spaces and tabs?  In the file there are spaces and just need to count the number of tabs.  

newfilename=$(awk 'NR==4 {print $8}' $file  "tab value" )

Thanks for all the help
BJE
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bje

ASKER

Thank you for all the help.  This is working great.