Set variables from fields in fields


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Set variables from fields in fields
# 1  
Old 09-24-2011
Set variables from fields in fields

Hi,
This is my first post here and I am a newbie. Smilie

I have a file that looks like this [web page title:filename]:

Introduction:Intro_123.html
Product definition:Prod_def.html
System Setup:SSetup-64bit.html
Setting up user accounts:Set_user_acc.html

I tried to create a script that would output "The filename is so-and-so and the title of the page is so-and-so". Well, my script looks like this:

Quote:
#!/bin/bash

while read file
do
title=$(cut -d ":" -f 1)
htmlfile=$(cut -d ":" -f 2)
echo "The name of the file is $htmlfile and the title is $title."
done<frep_titles
The output that I get is:

Quote:
The name of the file is and the title is Product definition
System setup
Setting up user accounts
I can't figure out what I am doing wrong. I have tried switching the lines that start with 'title' and 'htmlfile' around, and found that only the first ever get processed. Have also tried

while read line

instead of

while read file

...with no better results. Appreciate any help !
# 2  
Old 09-24-2011
Code:
$ cat myfile
Introduction:Intro_123.html
Product definition:Prod_def.html
System Setup:SSetup-64bit.html
Setting up user accounts:Set_user_acc.html

Code:
$ nawk -F: '{print "The filename is:",$1,"and the title of the page is:",$2}' myfile
The filename is: Introduction and the title of the page is: Intro_123.html
The filename is: Product definition and the title of the page is: Prod_def.html
The filename is: System Setup and the title of the page is: SSetup-64bit.html
The filename is: Setting up user accounts and the title of the page is: Set_user_acc.html

# 3  
Old 09-25-2011
Thanks for the quick reply! Smilie
# 4  
Old 09-25-2011
Or in shell:
Code:
while IFS=: read title htmlfile; do
  echo "The name of the file is $htmlfile and the title is $title."
done < infile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

3. Shell Programming and Scripting

Parsing fields into variables

A record contains 50 fields separated by "~". I need to assign each of these fields to different variables. Following is the shell script approach I tried. RECORD="FIELD1~FIELD2~FIELD3~FIELD4~FIELD5~...........~FIELD50" VAR1=$(echo ${RECORD} | cut -d"~" -f 1) VAR2=$(echo ${RECORD} | cut... (5 Replies)
Discussion started by: krishmaths
5 Replies

4. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

5. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

6. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

7. Shell Programming and Scripting

awk question ? set 2 variables by matching fields

Hello, I'm trying to get the TOP and BASE numbers printed out File looks like this: 2300 CAR # 2300 is the TOP 2310 CAR 2335 CAR 2455 CAR # 2455 is the BASE 1000 MOTOR # 2455 will become this TOP 2000 MOTOR 3000 MOTOR 4000 MOTOR # 4000 is the BASE 2345 BIKE # 4000... (8 Replies)
Discussion started by: charlieglen
8 Replies

8. Shell Programming and Scripting

Assigning fields values to different variables

Hi, I have this code: cat file.txt | awk -F, 'NR==1{print $6","$8","$10","$20","$21","$19}' > file.tmp VAR1=`cat file.tmp | cut -d "," -f1` VAR2=`cat file.tmp | cut -d "," -f2` VAR3=`cat file.tmp | cut -d "," -f3`; VAR4=`cat file.tmp | cut -d "," -f4`; VAR5=`cat... (1 Reply)
Discussion started by: Tr0cken
1 Replies

9. Shell Programming and Scripting

Add fields in different files only if some fields between them match

Hi everybody (first time posting here) I have a file1 that looks like > 1,101,0.1,0.1 1,26,0.1,0.1 1,3,0.1,0.1 1,97,0.5,0.5 1,98,8.1,0.218919 1,99,6.2,0.248 2,101,0.1,0.1 2,24,3.1,0.147619 2,25,23.5,0.559524 2,26,34,0.723404with 762 lines.. I have another 'similar' file2 > ... (10 Replies)
Discussion started by: murpholinox
10 Replies

10. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies
Login or Register to Ask a Question