defining a variable using multiple "entries/columns" within a line using read

Hello All,

I am trying to figure out how to use the read command. I have a txt file that has rows looking something like this:

1 2 3 4 5 6 7 8 9

where each number represents an entry of various characters deliminated by tabs.

My goal is to set entries 2-7 as a variable/string that I can use to grep within another file. I also want to be able to define entries 1, 8, and 9 seperately to print later into a second text file. I have tried researching this a bit and am a little confused about what a file descriptor is versus some scripts I have seen with lines like this:

while read line
do
F1=$(echo $line | cut -d$FS -f1)

1) Would -f1 correspond to the first entry of the line, -f2 the second entry, -f3 third and so on?

2) How would you specify IFS to be tab deliminated?

3) Would this be how I could set a variable to be multiple entries within a line:

while read line
do
MULTIPLE=$(echo $line | cut -d$FS -f2 -f3 -f4 -f5 -f6 -f7)

???

Thanks for the help!!!
Tori

This gives you two output files file1 with fields 2-7, file2 with fields 1,8,9 then greps.

awk '{ print $2, $3, $4, $5, $6, $7 > "file1"
          print $1, $8, $9 > "file2" } inputfile
# grep example
grep -f file1 someotherfile
grep -f file2 anotherfile

Hi Jim,

Thanks for the reply. The trouble is, that command is not preserving deliminators between $2 $3 etc. Maybe I should be a little more specific in what I am trying to do:

This is what my text file lookes like that I want to read line by line:

Text_file_1:
### SCAN ACQUISITION DATABASE: ###
#Headers: Protocol name, ok/err, Xdim, Ydim, Zdim, Nframes, unpack name, unpack format

# Martinos Center Protocols:
T1_MPRAGE_sag ok 256 256 128 1 3danat MGZ
T2TSEAXIAL ok 256 204 35 1 t2 MGZ
DIFFUSION_HighRes err 128 128 60 70 diff MGZ
T2_SPACE_sag_1mm_match ok 256 240 176 1 t2_space MGZ
T2_SPACE_sag_1mm_fl_match ok 256 240 176 1 t2_flair MGZ
MEMPRAGE_4e_p2_1mm_iso ok 256 256 176 1 memprage MGZ
gre_field_mapping ok 64 64 26 1 asl_field MGZ
PASL_PQII ok 64 64 26 121 asl MGZ
ge_funk-MR-Resting ok 64 64 26 191 ge_func MGZ

# VA Protocols:
MPRAGE ok 256 240 160 1 3danat MGZ
MPRAGERepeat ok 256 240 160 1 3danat MGZ
ep2d_diff_mddw_20_p2-MOD err 128 128 64 62 diff MGZ
AxialPD-T2TSE ok 256 228 48 2 AxialPD MGZ
ep2d_bold_moco ok 64 64 36 120 ge_func MGZ
ep2d_bold_mocoRepeat ok 64 64 36 120 ge_func MGZ
ep2d_bold_mocoFinger_tap ok 64 64 36 120 finger_tap MGZ
t2_spc_ns_sag_p2_da-fl_iso-MOD ok 256 256 176 1 t2_space MGZ
AXT2FLAIRFS ok 512 448 38 1 t2_flair MGZ

First, I only want to consider lines not starting with # (I assume using | grep -v "#"). I then want to use the first 6 entries from each line (seperated by tabs) in Text_file_1 to grep a line containing those same six entries in another Text_file_2. If it exists in the second file, I would like to print the entire line of the Text_file_2 into Text_file_3, but then use sed to replace some things.

Example:
Each Text_file_1 line:

MPRAGE ok 256 240 160 1 3danat MGZ

"MPRAGE ok 256 240 160 1" is then pulled out to grep within Text_file_2:

2 MPRAGE ok 256 240 160 1 30793230
3 MPRAGERepeat ok 256 240 160 1 30792546
4 ep2d_diff_mddw_20_p2-MOD err 128 128 64 62 30788600
10 AxialPD-T2TSE ok 256 228 48 2 30783957
11 ep2d_bold_moco ok 64 64 36 120 30782329
12 ep2d_bold_moco ok 64 64 36 120 30780996
16 ep2d_bold_mocoRepeat ok 64 64 36 120 30779689
17 ep2d_bold_mocoRepeat ok 64 64 36 120 30775121
21 ep2d_bold_mocoFinger_tap ok 64 64 36 120 30773814
22 ep2d_bold_mocoFinger_tap ok 64 64 36 120 30772481
26 t2_spc_ns_sag_p2_da-fl_iso-MOD ok 256 256 176 1 30771174
27 AXT2FLAIRFS ok 512 448 38 1 30767585

Since the first line of Text_file_2 matches, I want to then print it into a new textfile in the following format:

2 MPRAGE ok 256 240 160 1 30793230 (Text_file_2)
combined with
MPRAGE ok 256 240 160 1 3danat MGZ (text_file_1)

To create a new file with a line that looks like this:
2 3danat MGZ 002.mgz

I am more familiar with csh, but I cannot find a way to use a text file line by line using csh. Therefore, my idea is to call a bash script within my csh script to make my new text file. I don't even know if that will work. I really apologize if this is confusing. If I am not being clear, I can go back to the old way of doing things. I'm just trying to find a way not to hard code a script.

Thanks again!
Tori

Ooops, I forgot the commas between $1, $2 etc, which is why it was not printing spaces. That actually helps alot. :slight_smile: