LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-01-2006, 07:05 AM   #1
sharapchi
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: slackware
Posts: 119

Rep: Reputation: 15
retrieve a line from a file


Do you know a command in linux to retrieve specified line from a file?

Thanks.
 
Old 12-01-2006, 07:25 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You can use sed for this. e.g. print line number 42:
Code:
sed -n '42 p' your_file_name
 
Old 12-03-2006, 08:12 AM   #3
sharapchi
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: slackware
Posts: 119

Original Poster
Rep: Reputation: 15
For example I have file like

bla1 ddd1
bla2 eee2
bla3 fff3

how i can retrive specified word on a specified line?

for exapmle I want to retrive second word on third line? (fff3)
 
Old 12-03-2006, 08:33 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by sharapchi
For example I have file like

bla1 ddd1
bla2 eee2
bla3 fff3

how i can retrive specified word on a specified line?

for exapmle I want to retrive second word on third line? (fff3)
Do you want to retrieve based on name or location? For the former, just use grep, eg:
cat <filename>|grep <keyword>
returns every line in <filename> which contains <keyword>. For more sophisticated stuff, read up on sed and awk. I would start with the Bash Guide for Beginners by Machtelt Garrels--its on tldp.org.
 
Old 12-03-2006, 08:45 AM   #5
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Code:
sed -n '42 p' your_file_name |  awk '{print $2}'
That will retrieve the second word from the forty second line. Change 42 to the line you want and change $2 to the word you want.
 
Old 12-03-2006, 01:20 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
If you are looking to extract part of a line, using awk as Andrew mentioned is a good method when the you have fields de-limited by whitespace. If you have a delimited records on some othere character, you can use awk with the -F option. For example to print the third field as delimited by a colon character:
Code:
... |awk -F: '{ print $3 }'
.
Delimited fields can alse be extracted using the cut command, which is lighter weight than awk. You specify the delimiting character with the -d option, and the field number(s) with the -f option. You can do the same as the awk command above with cut like this:
Code:
... | cut -d: -f3
Cut can also extract by character position:
Code:
... | cut -c13-19
...will print output characters 13-19 of the input line(s).

There are several other options. See the cut manual page for more information.
 
Old 12-03-2006, 03:19 PM   #7
sharapchi
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: slackware
Posts: 119

Original Poster
Rep: Reputation: 15
Third question, how can i change the data at specified location? For example my data file like this :

bla1 ddd1
bla2 eee2
bla3 fff3

How can i change the words in second word in second line? (eee2)
Do i have to use bash programming?

Last edited by sharapchi; 12-03-2006 at 03:31 PM.
 
Old 12-03-2006, 03:44 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You could do it with sed - the stream editor. It's unclear how you with to identify the line... by the line number, by some pattern...? I chose to do it by the pattern eee2 here, but there are other ways depending on how you want it to work.
Code:
sed -i '/eee2/ s/eee2/something else/' yourfilename
 
Old 12-03-2006, 04:39 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by sharapchi
Third question, how can i change the data at specified location? For example my data file like this :

bla1 ddd1
bla2 eee2
bla3 fff3

How can i change the words in second word in second line? (eee2)
Do i have to use bash programming?
Maybe not, but it is the easiest.
Did you read the bash tutorial I suggested?

I recommend asking the whole question up front--not in little pieces.
 
Old 12-03-2006, 04:44 PM   #10
sharapchi
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: slackware
Posts: 119

Original Poster
Rep: Reputation: 15
I want to write a bash code but i cannot know how to use bash variable. For example :

#fileoperations.sh

getline()
{
sed -n '$2 p' $1 #???
}

And also in awk..

Last edited by sharapchi; 12-03-2006 at 04:46 PM.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
for loop to retrieve info and write approbiate line of code? ati Programming 2 05-07-2006 11:07 AM
Retrieve Window ID in command line.. joely2k Linux - General 3 11-26-2005 11:44 AM
Retrieve a file from another server bijuhpd Linux - Newbie 2 09-29-2005 12:04 AM
Retrieve only a single line from a file rose_bud4201 Programming 4 06-22-2005 06:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:18 PM.

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
Open Source Consulting | Domain Registration