reading alternate lines of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading alternate lines of a file
# 1  
Old 08-04-2010
Bug reading alternate lines of a file

hi,
i have 2 files.
file1:
Code:
1
2
3
4
5
6

file2:
Code:
a
b
c
d
e
f
g
h
i
j

i want output in file3 as
Code:
1
b
3
d
5
f
h
j

i need a shell script program to this task.please help.

Last edited by Scott; 08-04-2010 at 06:21 AM.. Reason: Code tags
# 2  
Old 08-04-2010
What have you tried so far and what is the "real world problem" of your question?
# 3  
Old 08-04-2010
Hi

Code:
paste -d"\n" file1 file2 | awk '(NR%4==1 || NR%4==0) && $0'

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 4  
Old 08-04-2010
thanks a lot. before i use to do like this

Code:
$vi f1.sh
paste -d'\n' nice  | while read line2;
do
                echo "$line2"
                read line2
done

$vi f2.sh

paste -d'\n' nice  | while read line2;
do
                echo "$line2"
                read line2
done

$sh newfile.sh >> f1

$sh newfile1.sh >> f2

$vi final.sh
paste -d'\n' f1 f2 | while read line1 && read line2;
do
echo "$line1"
echo "$line2"
done

$sh final.sh
1
a
3
c
5
e
7
g
9
I

your option is brilliant thanks

Last edited by Scott; 08-04-2010 at 06:22 AM.. Reason: Please use code tags
# 5  
Old 08-09-2010
explanation to answer

hello
could you please explain this command?

Code:
awk '(NR%4==1 || NR%4==0) && $0'


Last edited by Scott; 08-09-2010 at 06:22 AM.. Reason: Code tags, please...
# 6  
Old 08-09-2010
Hi
It means: Group the output as 4 lines each, and print the 1st(NR%4==1) and 4th line(NR%4==0) if it is not empty.

Guru.
This User Gave Thanks to guruprasadpr For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing alternate lines of code

Hi gents, Have only a passing familiarity with linux/shell at this point, so please forgive simple question. I have text files that have lines something like the following: a b c d d d e f e f e f a b (6 Replies)
Discussion started by: cabled
6 Replies

2. Shell Programming and Scripting

Process alternate lines in awk/sed/perl

hi.. i have a fasta file with the following format >sequence1 CCGGTTTTCGATTTGGTTTGACT >sequence2 AAAGTGCCGCCAGGTTTTGAGTGT >sequence3 AGTGCCGCAGAGTTTGTAGTGT Now, i want to read alternate line and add "GGGGGGGGGGG" to end of every sequence Desired output: >sequence1... (4 Replies)
Discussion started by: empyrean
4 Replies

3. Shell Programming and Scripting

Grep values on alternate lines

Hi, I have a file like 2011|ACC|.* 2013|ACC|.* 2011|ACCC|.* 2013|ACCC|.* 2013|ACCV|.* 2011|ADB|.* 2013|ADB|.* 2011|ADBC|.* 2013|ADBC|.* 2011|AIA|.* 2013|AXJ|.* 2013|NNN|.* .* represnts any alphanumeric characters after this part of the string I need a code to return only the... (3 Replies)
Discussion started by: sam05121988
3 Replies

4. Programming

Perl : joining alternate lines

Hi, I need to join every alternate line in a file for eg:input file $ cat abc abc def ghi jkloutput abc def ghi jklcode i wrote for this $ cat add_line.pl #!/usr/bin/perl -w my $count=1; #my $line=undef; my @mem_line; my $i=0; my $x=0; (2 Replies)
Discussion started by: sam05121988
2 Replies

5. Shell Programming and Scripting

Regarding reading lines into a new file

Hi all, I jut use a loop to read lines from the user and redirect it to a file. echo "Enter the line" while read -r LINE do echo $LINE >> FILE if ;then break fi done input app... (1 Reply)
Discussion started by: Ananthdoss
1 Replies

6. Shell Programming and Scripting

skip lines while reading a file

Hi Experts, I am tryin to read a file and while doing so i need to skip the lines which start with a hash (#) char. I thought of using a goto command but a lot of guys on this site say its not the good way to program. Moreover I am using a ksh shell which deos not support goto command. ... (4 Replies)
Discussion started by: bankimmehta
4 Replies

7. Shell Programming and Scripting

Insert string in alternate lines

Hi All, In continuation of my previous thread 'Add text at the end of line conditionally', I need to further modfiy the file after adding text at the end of the line. Now, I need to add a fixed charater string at alternate lines starting from first line using awk or sed.My file is now as below:... (10 Replies)
Discussion started by: angshuman
10 Replies

8. Shell Programming and Scripting

alternate lines

Hi, I'm new to Unix. I want to read the all the lines from a text file and write the alternate lines into another file. Please give me a shell script solution. file1 ----- one two three four five six seven newfile(it should contain the alternate lines from the file1) ------- one... (6 Replies)
Discussion started by: pstanand
6 Replies

9. UNIX for Dummies Questions & Answers

skip reading certain lines in a file

How can I exclude reading lines in a file that contains the following: filesystem:/home/pach/liv_patches 128005120 88456640 37270758 71% /home/patches That is, all lines that contain and begins with filesystem: should not be processed/read from a file (5 Replies)
Discussion started by: paulsew
5 Replies

10. UNIX for Dummies Questions & Answers

alternate lines from two files

A basic request two files want to combine them but on alternate lines (1 Reply)
Discussion started by: SummitElse
1 Replies
Login or Register to Ask a Question