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
Alternate way for echo. senthil_is Shell Programming and Scripting 5 03-05-2008 09:12 AM
alternate rows of a file dr46014 Shell Programming and Scripting 3 08-25-2007 06:16 AM
Alternate command for cut Mohammed Shell Programming and Scripting 2 10-20-2006 07:42 AM
Alternate to RARP Solaris 5.6 & 5.8 dmerg59 IP Networking 2 06-28-2005 08:28 PM
alternate lines from two files SummitElse UNIX for Dummies Questions & Answers 1 02-24-2005 09:54 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-17-2008
pstanand pstanand is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 10
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
three
five
seven

please let me know a solution

Thanks in advance
Pstanand
  #2 (permalink)  
Old 03-17-2008
sanjaypraj sanjaypraj is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 7
You can try this shell script

rm -f fileout
i=1
while read line
do
if [[ $i -eq 1 ]]
then
echo $line >>fileout
i=0
continue
fi
if [[ $i -eq 0 ]]
then
i=$((i+1))
continue
fi
done<$filename
  #3 (permalink)  
Old 03-17-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 166
Code:
$ cat file1
one
two
three
four
five
six
seven

$ awk 'NR%2 {print > "newfile"}' file1

$ cat newfile
one
three
five
seven
//Jadu
  #4 (permalink)  
Old 03-18-2008
pstanand pstanand is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 10
Hi Sanjay,

Thank you very much. It works fine. But when I try Jadu code I got the following error.

awk: syntax error near line 1
awk: bailing out near line 1

Can you people pls tell me why this happening?

Regards
pstanand
  #5 (permalink)  
Old 03-18-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,861
Try the following awk and sed scripts

Code:
awk 'NR%2' file1 > newfile
Code:
sed -n '1,${p;n;}' file1 > newfile
  #6 (permalink)  
Old 03-18-2008
pstanand pstanand is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 10
HI fpmurphy,
thanks it is great working fine. Can you please explain me how this works?

Regards
pstanand
  #7 (permalink)  
Old 03-18-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,047
Code:
sed -n 'p;n' filename -- print the odd line
sed -n 'n;p' filename -- print the even line
Sponsored Links
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 07:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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