Skip first and last line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Skip first and last line
# 1  
Old 03-05-2013
Skip first and last line

Hi All

I have a sample file like below:

Code:
012312112                                   
1372422843      1236712                     
1372422843      1275127                     
3109301010

from which I wan't to:
1.)delete the first and last line
2.)starting from the second position, cut 15 digits from the first column(including spaces)

for 1.) The command
Code:
sed -e '1d' -e '$d' file

is only deleting the first but not the last line
for 2.)
Code:
cut -c2-16 file

Is there a way to do this with a single statement?

Thanks
# 2  
Old 03-05-2013
Using awk program:
Code:
awk -v L=$(wc -l < file) 'NR==1||NR==L{next}{print substr($0,2,15) } ' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-05-2013
Another approach:
Code:
awk 's{print substr(s,2,15}NR-1{s=$0}' file


Last edited by Franklin52; 03-05-2013 at 10:54 AM..
This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 03-05-2013
Code:
head -n -1 file | tail -n +2

# 5  
Old 03-05-2013
Thank you Franklin...it's working but I want the columns to be removed from the original file itself..so the output should look something like


Code:
1      1236712                     
1      1275127

Thanks
# 6  
Old 03-05-2013
Using ksh script:
Code:
#!/bin/ksh

lines=`cat $file|wc -l`
((lines=$lines-1))
if [ $lines -ge 1 ]
then
   tail +2 $file | head -$lines | cut -f2- -d" "
else
   print "No records"
fi

Not all implementations support all the flags of head

I know that the cat $file|wc -l bit looks like a UUOC, but different OS can give a different output formats.

I'm not sure where the "1" in the first column of your output comes from. Can you explain?


I hope that this helps.

Robin,
Liverpool/Blackburn
UK
# 7  
Old 03-05-2013
Hi rbatte1

the original file is this:

Code:
012312112                                   
1372422843      1236712                     
1372422843      1275127                     
3109301010

the amended one should look like this

Code:
1      1236712                     
1      1275127

after the removal of the header and footer and the column 1 cut from the 2nd position onwards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Skip to next line if the variable is string

Hi I have the follwoing requirement I have a file as follows: # cat priy yyy.poweroff_cmd = /sbin/poweroff hhh.powersave-nap = 1 When this file is provided as input, I first used "awk" command and saved variables present after "=" replace=$line replace1=`echo $line | awk -F "="... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

2. Red Hat

How to start Fedora 11 in command line mode and skip damaged programs ??

Hi All, Please let me know that how to start Fedora 11 in command line mode and skip damaged programs ?? Scenario being: I have Fedora 11 ( pretty ole... eh !! ). If I try to start the PC , then after some steps of startup... it just hangs and does not boots. I tried entering the mode... (4 Replies)
Discussion started by: dipanchandra
4 Replies

3. UNIX for Advanced & Expert Users

Read file and skip the line starting with #

Hi all, I'm new in unix. Need some help here. I have a file called server.cfg which contains the servers name, if I don't want to run on that server, I'll put a "#" infront it. username1@hostname.com username2@hostname.com #username3@hostname.com #username4@hostname.com... (17 Replies)
Discussion started by: beezy
17 Replies

4. Shell Programming and Scripting

sed command to skip the first line during find and replace operation

Hi Gurus, I did an exhaustive search for finding the script using "sed" to exclude the first line of file during find and replace. The first line in my file is the header names. Thanks for your help.. (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. Shell Programming and Scripting

want to skip a line in XML file using awk

HI All, I am trying to split a xml using awk. now the issue is i want to skip three lines from the xml file. first two and last one based on pattern. plz some one help. i am new to awk and struggling :wall: <?xml version="1.0"?> <notification> ..... ..... ..... ..... ........ (24 Replies)
Discussion started by: ganesan kulasek
24 Replies

6. Shell Programming and Scripting

How to skip the first line of the script in shell using python?

How to skip first line of the script in shell, using python. (3 Replies)
Discussion started by: KarthikPS
3 Replies

7. UNIX for Dummies Questions & Answers

skip first line when doing a read of csv file

Folks, how do i skip the first line in a csv, while doing the read of a csv file in to a variable line by line. eg : do echo $line done < $rpt where rpt is path to csv file The initial 1st line is a garbage that i want to avoid, and start reading from 2nd line ... (2 Replies)
Discussion started by: venu
2 Replies

8. Shell Programming and Scripting

Replace all but skip first instance in a line

I have a record like the one given below. 010000306551~IN ~N~ |WINDWARD PK|Alpharetta| If ~ is present more than instance in a line,then I need to delete those instances. Any ideas? I am working in Solaris (7 Replies)
Discussion started by: prasperl
7 Replies

9. Shell Programming and Scripting

Output of Shell Script Doesn't Skip to next line

Hi. This is what I coded: tput cup $1 $2 # place cursor on row and col tput clear # clear the screen bold=`tput smso` #set stand-out mode - bold offbold=`tput rmso` # reset screen - turn bold off echo $bold # turn bold on tput cup 10 20; echo "Type Last Name:" #bold caption tput cup 12... (3 Replies)
Discussion started by: Ccccc
3 Replies

10. Shell Programming and Scripting

Skip new line

Hi, how can I skip the new line of echo? In SH!!!! echo "the date is :" date and result I want is the date is : Tue Oct 11 22:24:37 WEST 2005 I've already tried including the \c inside the echo, but it didn't work. Thanks! (2 Replies)
Discussion started by: pmpx
2 Replies
Login or Register to Ask a Question