How to read the first work of each line from text file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read the first work of each line from text file?
# 1  
Old 12-13-2012
How to read the first work of each line from text file?

Hi ppl.

I want to read the first word of each line of a text file to a variable in a bash script.

Can anyone help pls, thanks.
# 2  
Old 12-13-2012
For reading 1st word:

Code:
temp11=$(awk '{print $1}' file_name.txt)
echo $temp11

OR

Code:
temp2=$(cut -d " " -f1 file_name.txt)
echo $temp2

For reading 1st character:

Code:
temp=$(cut -c 1 file_name.txt)
cat $temp

I hope this helps.

Last edited by manishdivs; 12-13-2012 at 08:23 PM.. Reason: Adding another code
This User Gave Thanks to manishdivs For This Post:
# 3  
Old 12-13-2012
another example:
Code:
while read var x
do
  echo $var
done < infile

# 4  
Old 12-15-2012
Quote:
Originally Posted by manishdivs
For reading 1st word:

Code:
temp11=$(awk '{print $1}' file_name.txt)
echo $temp11

OR

Code:
temp2=$(cut -d " " -f1 file_name.txt)
echo $temp2

For reading 1st character:
Both work fine, many thanks! And seems so simple. Have to look at the awk command to understand it.

I was trying to cycle something like this:
Code:
cat ../data/SP500symbols.txt | sed -n '1p'

But because I'm a newbie to bash didn't manage to make the line cycle work...


Now I only need to read each word at a time from the $temp11 variable. I want to use each word to construct an HTTP address and use curl to download a file for each one.

Can anyone help me on using cycles with variables. I don't really understand the use of variables. Are there different variable types? I'm used to C and matlab scripting only...

thanks again
# 5  
Old 12-16-2012
Deploy rdrtx1's proposal. It will cycle through the file, reading each line's first word into var, until file is done. Put your commands into the loop.
Your sed construct will print the entire first line, that's it. To gather the first word (into another file), use sth like
Code:
 sed  's/^\([^ \t]*\).*/\1/' file

It will collect any non-space char (i.e. the first word) at the start of line, and then the rest of the line, replace the line with those non-spaces, and print.
# 6  
Old 12-16-2012
Quote:
Originally Posted by RudiC
Deploy rdrtx1's proposal. It will cycle through the file, reading each line's first word into var, until file is done. Put your commands into the loop.
Your sed construct will print the entire first line, that's it. To gather the first word (into another file), use sth like
Code:
 sed  's/^\([^ \t]*\).*/\1/' file

It will collect any non-space char (i.e. the first word) at the start of line, and then the rest of the line, replace the line with those non-spaces, and print.
Thanks RudiC but I'm already using this:
Code:
address=$(echo "http://www.google.com/finance/getprices?i=60&p=1d&f=d,o,h,l,c,v&df=cpct&q=")

#read the name of each SP500 company
symbolsList=$(awk '{print $1}' ../data/SP500symbols.txt)

for symbol in $symbolsList
do 
  echo $symbol;
  fullAdd=$(echo $address$symbol)
  
#curl -o saveDir httpAddr

done

It's simple and works fine.

Not only need to think a little on how to use curl and make the script run every day to save the data files.

Nice Sunday!
# 7  
Old 12-17-2012
If you're happy with what you posted, fine. Hint: you dont need the echo and command substitution for simple variable assignments (address & fullAdd).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies

2. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

3. Programming

Read text from file and print each character in separate line

performing this code to read from file and print each character in separate line works well with ASCII encoded text void preprocess_file (FILE *fp) { int cc; for (;;) { cc = getc (fp); if (cc == EOF) break; printf ("%c\n", cc); } } int main(int... (1 Reply)
Discussion started by: khaled79
1 Replies

4. Shell Programming and Scripting

how read specific line in a file and write it in a new text file?

I have list of files in a directory 'dir'. Each file is of type HTML. I need to read each file and get the string which starts with 'http' and write them in a new text file. How can i do this shell scripting? file1.html <head> <url>http://www.google.com</url> </head> file2.html <head>... (6 Replies)
Discussion started by: vel4ever
6 Replies

5. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

6. Shell Programming and Scripting

Read text file line by line

Hi everyone, I am writing a BASH shell script that I will use to process data files. I have a text file that contains the names of the files to be processed, for example: cat filenames.txt file1 file2 file3 file4 file5 What I would like to do is set up a FOR loop within my script... (2 Replies)
Discussion started by: msb65
2 Replies

7. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

8. Shell Programming and Scripting

Read random line from a text file

I have a text file with hundreds of lines, i wish to run a script and reads a random line to pass it to another command line such as: for line in `cat file |grep random line`; do echo $line |mail my@example.com ; done thank you (6 Replies)
Discussion started by: Bashar
6 Replies

9. Shell Programming and Scripting

Script does not read the last line of text file

Hello, I have got a script that reads a text file, and have got three problems that I an struggling with. 1. The script does not read the last line in the text file 2. within the second 'elif' within the script I included a 'break' - the script runs successfully (except for the first... (2 Replies)
Discussion started by: jermaine4ever
2 Replies

10. Shell Programming and Scripting

Read from text file misses first line

Hi! I need to read in the first line from a text file (which will only ever have one line in it), so I tried this.... while read line do echo $line done < $file But this wasn't returning anything. So I tired a different file, which had multiple lines of text in it, and it returned... (2 Replies)
Discussion started by: davewg
2 Replies
Login or Register to Ask a Question