Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

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;;
0
bje
Asked:
bje
  • 3
  • 3
1 Solution
 
woolmilkporcCommented:
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
0
 
bjeAuthor Commented:
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?
0
 
woolmilkporcCommented:
Yes.

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

"NR" is an automatic counter for "Number of Records" in awk.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bjeAuthor Commented:
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
0
 
woolmilkporcCommented:
Use

awk -F"\t" 'NR== ...
0
 
bjeAuthor Commented:
Thank you for all the help.  This is working great.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now