The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
insert multiple lines into a file c0mrade Shell Programming and Scripting 12 09-04-2008 04:04 PM
Adding Multiple Lines to Multiple Files dayinthelife Shell Programming and Scripting 2 06-04-2008 11:50 AM
How to copy multiple lines from a file to another using AWK? jisha Shell Programming and Scripting 3 01-08-2008 01:04 AM
Need to delete multiple lines in a file. kangdom Shell Programming and Scripting 6 10-16-2006 11:02 AM
Add Multiple Lines in an existing file hkhan12 Shell Programming and Scripting 5 09-08-2006 11:11 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-13-2008
dala dala is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 7
retrieved multiple lines on multiple places in a file

I have a file containing the following lines:

first line
second line
third line
something goes here
something else goes here
something goes here
first line1
second line2
third line3

I need to go through the file and retrieved these lines and print them the output should look like this:

first line
second line
third line
first line1
second line2
third line3

Please help me get start it. I used while loop but the output wasn’t what I want it.
  #2 (permalink)  
Old 02-13-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
How do you know which lines you want and which you don't?

Let's say you want 3 lines, skip 3 lines, etc. Here's just one way (using GNU awk):

Code:
 awk '(int((FNR-1)/ 3))%2 == 0'  filename
  #3 (permalink)  
Old 02-14-2008
dala dala is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 7
Quote:
Originally Posted by otheus View Post
How do you know which lines you want and which you don't?

Let's say you want 3 lines, skip 3 lines, etc. Here's just one way (using GNU awk):

Code:
 awk '(int((FNR-1)/ 3))%2 == 0'  filename
Thx otheus, to answer your question, I am using `grep "^first"`, when I found it I would continue and grep for the "^second" word and so on.
  #4 (permalink)  
Old 02-14-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
Quote:
Originally Posted by dala View Post
Thx otheus, to answer your question, I am using `grep "^first"`, when I found it I would continue and grep for the "^second" word and so on.
Okay, so you can use "egrep" for a more interesting regular expression:

Code:
egrep "^(first|second|third) " infile.txt
if that doesn't work, just use awk with almost identical syntax:

Code:
awk "/^(first|second|third) /" infile.txt
  #5 (permalink)  
Old 02-14-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,524
Quote:
Originally Posted by dala View Post
Please help me get start it. I used while loop but the output wasn’t what I want it.
Code:
while  read -r line
do
    case $line in
        first*|second*|third* ) echo "$line";;
    esac 
done < "file"
  #6 (permalink)  
Old 02-14-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
Quote:
Originally Posted by ghostdog74 View Post
Code:
while  read -r line
do
    case $line in
        first*|second*|third* ) echo "$line";;
    esac 
done < "file"
That looks like it should work, though like I said, the grep/awk solution would be more efficient. Maybe you should put double-quotes around the $line, as in:
Code:
case "$line" in
In ksh and bash, you can also parse out the first word from the rest:
Code:
case "${line%% *}" in
   first|second|third) echo "$line";;
esac
  #7 (permalink)  
Old 02-14-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,524
Quote:
Originally Posted by otheus View Post
In ksh and bash, you can also parse out the first word from the rest:
Code:
case "${line%% *}" in
   first|second|third) echo "$line";;
esac
Code:
while read -r a b
do
 case $a in 
  ....
 esac
done
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0