retrieved multiple lines on multiple places in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting retrieved multiple lines on multiple places in a file
# 1  
Old 02-13-2008
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  
Old 02-13-2008
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  
Old 02-14-2008
Quote:
Originally Posted by otheus
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  
Old 02-14-2008
Quote:
Originally Posted by dala
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  
Old 02-14-2008
Quote:
Originally Posted by dala
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  
Old 02-14-2008
Quote:
Originally Posted by ghostdog74
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  
Old 02-14-2008
Quote:
Originally Posted by otheus
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a string and add " at multiple places

Hi All, I have below text, i want to add " in it, please advise. FORD MOTOR COMPANY FORD COMPONENT SALES, LLC Mazda Motor Manufacturing de Mexico, S.A. de C.V. Desired: "FORD MOTOR COMPANY" "FORD COMPONENT SALES, LLC"" Mazda Motor Manufacturing de Mexico, S.A. de C.V." edit by bakunin:... (2 Replies)
Discussion started by: Amit Saroha
2 Replies

2. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

3. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

4. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

5. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

6. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

7. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

8. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

9. Shell Programming and Scripting

Logfile parsing with variable, multiple criterias among multiple lines

Hi all I've been working on a bash script parsing through debug/trace files and extracting all lines that relate to some search string. So far, it works pretty well. However, I am challenged by one requirement that is still open. What I want to do: 1) parse through a file and identify all... (3 Replies)
Discussion started by: reminder
3 Replies

10. Shell Programming and Scripting

Find multiple patterns on multiple lines and concatenate output

I'm trying to parse COBOL code to combine variables into one string. I have two variable names that get literals moved into them and I'd like to use sed, awk, or similar to find these lines and combine the variables into the final component. These variable names are always VAR1 and VAR2. For... (8 Replies)
Discussion started by: wilg0005
8 Replies
Login or Register to Ask a Question