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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting defining a variable using multiple "entries/columns" within a line using read
# 1  
Old 04-20-2009
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
# 2  
Old 04-20-2009
This gives you two output files file1 with fields 2-7, file2 with fields 1,8,9 then greps.
Code:
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

# 3  
Old 04-20-2009
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

Last edited by Torinator; 04-20-2009 at 03:40 PM..
# 4  
Old 04-20-2009
Ooops, I forgot the commas between $1, $2 etc, which is why it was not printing spaces. That actually helps alot. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Using "while" to read columns in a file

Legends, i am using while to read 4 colums from a file. but the while loop exits after reading 1st column. please let me know, how to reach till end of the file. FILE CONTENT birdiex1:/home/s/sandeepk/scripts> vi /tmp/gleod_time "/tmp/gleod_time" 03_upd_stat_tbl_birdiex1 ... (2 Replies)
Discussion started by: sdosanjh
2 Replies

4. Shell Programming and Scripting

AWK for multiple line records RS="^" FS="#"

I have to pull multiple line records with ^ as the record separator(RS)... # should be my field separator (FS)... Sample record is: ^-60#ORA-00060: deadlock detected while waiting for resource ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated#PROC:AVAILABLE_FOR_GETNXTTIC#02/27/2012... (7 Replies)
Discussion started by: Vidhyaprakash
7 Replies

5. Programming

fgets read file line with "\n" inside

Hi, I have a string like this, char str ="This, a sample string.\\nThis is the second line, \\n \\n, we will have one blank line"; if I want to use strtok() to seperate the string, which token should I use? I tried "\n", "\\n", either not working. peter (1 Reply)
Discussion started by: laopi
1 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

8. Shell Programming and Scripting

script to read a line with spaces bet " " and write to a file

Hi, I need a command in UNIX KSH below is the description... MAPPING DESCRIPTION ="Test Mapping for the calid inputs" ISVALID ="YES" NAME ="m_test_xml" OBJECTVERSION ="1" VERSIONNUMBER ="1" unix ksh command to read the DESCRIPTION and write to a file Test Mapping for the calid inputs... (3 Replies)
Discussion started by: perlamohan
3 Replies

9. Shell Programming and Scripting

Awk - to test multiple files "read" permission ?

Hi Masters, Iam new to this Forum and this is my first post. My question is: I've some datafiles belongs the type (A, B, C) in the location 'export/home/lokiman ' dataA1.txt dataB28.txt dataC35.txt 1) I've to check the read permission for each file, if it not there then I've to... (1 Reply)
Discussion started by: lokiman
1 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question