LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2012, 08:59 PM   #1
drandre
LQ Newbie
 
Registered: Apr 2012
Distribution: OpenSUSE 12.1
Posts: 11

Rep: Reputation: 0
File Processing in a Script


First time poster, long time lurker. You folks have a great board here. My question is as follows:

I have a file with five entries, each on a separate line. I also have a script that needs to grab the fourth entry from the file without knowing what the entry contains (eg. the entry cannot be explicitly written in the script).

If I throw the file into a do-while loop, I can process each line. I'm thinking that if I can somehow pair each line with an incremented number or variable, or create an array, I can somehow grab the fourth entry.

Thoughts?
 
Old 04-17-2012, 09:47 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
You can use an array and assign each line in your file to an element in the array:
Code:
myarray=($(cat yourfile))
Arrays are zero-based, so to print the 4th element:
Code:
echo ${myarray[3]}
 
1 members found this post helpful.
Old 04-17-2012, 09:55 PM   #3
drandre
LQ Newbie
 
Registered: Apr 2012
Distribution: OpenSUSE 12.1
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by towheedm View Post
You can use an array and assign each line in your file to an element in the array:
Code:
myarray=($(cat yourfile))
Arrays are zero-based, so to print the 4th element:
Code:
echo ${myarray[3]}
That code solves my problem perfectly. Thank you for your help!
 
Old 04-19-2012, 01:28 AM   #4
abhinav4
Member
 
Registered: Jun 2010
Location: India
Distribution: Fedora/Cent OS
Posts: 123

Rep: Reputation: 0
If i am not wrong this could be more easier form

sed -n 4,4p filename
 
Old 04-19-2012, 01:48 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Quote:
Originally Posted by abhinav4 View Post
If i am not wrong this could be more easier form

sed -n 4,4p filename
If I look at the OP's question I do believe you are correct.

You can shorten the solution you provided a bit:
Code:
sed -n 4p infile
 
Old 04-19-2012, 01:28 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Bash from version 4 also has the mapfile built-in command for reading lines into an array, and it has an option for setting the initial index value, so line one can be index one.

Code:
mapfile -t -O 1 array <file
echo "${array[4]}"
Or if you want to do it the hard way, try something like this:

Code:
x=1
while read line ; do
	(( x == 4 )) && echo "$line" && break
	(( x++ ))
done <file
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Versioned file processing script yungblood Linux - General 2 03-05-2011 11:25 AM
shell script for processing file: RudraB Programming 8 06-30-2010 02:13 AM
How to processing the log file within certain dates based on the file name shyork2001 Linux - General 1 04-08-2010 04:35 PM
script help for batch processing mma8x Linux - General 5 12-08-2006 02:31 AM
Java File Processing using Scanner; Can't get it to read file in running directory xemous Programming 2 09-26-2006 07:13 PM


All times are GMT -5. The time now is 08:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration