Redirecting output from Nth line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirecting output from Nth line
# 1  
Old 08-14-2018
Redirecting output from Nth line

Dear All,

I have a shell script which output like some thing below

Code:
1.2.3.4
1.2.3.5
1.2.3.6
------
Start of CSV
-------
id ,number ,name ,location
1,101,asp,xyz
2,102,dsp,ert

Now i need to redirect this output to csv file but from particular line number. i mean, first i need to skip first 3 lines , redirect rest to csv output

Code:
$> du.sh >  log.csv

Where I need to skip first 3 lines which
Code:
1.2.3.4
1.2.3.5
1.2.3.6

but only redirect from line 4..

can you please guide me
Moderator's Comments:
Mod Comment Please use CODE tags (as required by forum rules) when displaying sample input, sample output, and code segments.
# 2  
Old 08-14-2018
Please use code tags.

tail:
Code:
du.sh | tail -n +4

sed:
Code:
du.sh | sed '1,3d'

awk:
Code:
du.sh | awk 'NR>3'

There may be others.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 3  
Old 08-14-2018
Hi andrew,

Sed works..thank you but I need one more suggestion

I am getting extra , (comma) at end of line, so i need to remove it.. so trying below, but it saying syntax error line 2. at > /home/centos/abc.csv
Code:
#!/bin/bash
sh /home/centos/du.sh | sed 1,124d > /home/centos/abc.csv
for fname in abc.csv
   do
   cat $fname | sed 's/.$//' > tmp.tmp
   mv tmp.tmp $fname
done

------ Post updated at 07:24 AM ------

its working now... used , instead of . in sed
# 4  
Old 08-14-2018
A bit overcomplicated, no? A for loop for a single file, two sed invocations in lieu of one. . . Why not (untested)

Code:
sh /home/centos/du.sh | sed '1,124d; s/.$//' > /home/centos/abc.csv

This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-14-2018
Quote:
Originally Posted by RudiC
A bit overcomplicated, no? A for loop for a single file, two sed invocations in lieu of one. . . Why not (untested)

Code:
sh /home/centos/du.sh | sed '1,124d; s/.$//' > /home/centos/abc.csv

This works too Smilie
# 6  
Old 08-15-2018
Quote:
Originally Posted by RudiC
A bit overcomplicated, no? A for loop for a single file, two sed invocations in lieu of one. . . Why not (untested)

Code:
sh /home/centos/du.sh | sed '1,124d; s/.$//' > /home/centos/abc.csv

Or simply invert the sed printing to have one command:
Code:
sh /home/centos/du.sh | sed -n '125,$ s/,$//p'

Andrew
This User Gave Thanks to apmcd47 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

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

3. Shell Programming and Scripting

Redirecting the output

For example, if we run the below command, symcfg list -thin -pool , results in an output most of the times and if the out is generated i'm able to redirect the output to a file. but sometimes it doesnt result any output and even though the output is being redirected, i can see "No Thin Pools "... (2 Replies)
Discussion started by: web2moha
2 Replies

4. Shell Programming and Scripting

Extracting lines after nth LINE from an output

Hi all, Here is my problem for which i am breaking my head for past three days.. I have parted command output as follows.. Model: ATA WDC WD5000AAKS-0 (scsi) Disk /dev/sdb: 500GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type ... (3 Replies)
Discussion started by: selvarajvs
3 Replies

5. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

6. Shell Programming and Scripting

How to start reading from the nth line till the last line of a file.

Hi, For my reuirement, I have to read a file from the 2nd line till the last line<EOF>. Say, I have a file as test.txt, which as a header record in the first line followed by records in rest of the lines. for i in `cat test.txt` { echo $i } While doing the above loop, I have read... (5 Replies)
Discussion started by: machomaddy
5 Replies

7. UNIX for Dummies Questions & Answers

redirecting script output

Hello, I am interested in taking the output from a script i wrote and using it as input to a different script i wrote. So for example i want to take the output from program2 and use it as a parameter for program1. I didnt think i could use the >> symbols because i think that is just for .txt... (4 Replies)
Discussion started by: GmGeubt
4 Replies

8. Shell Programming and Scripting

extract nth line of all files and print in output file on separate lines.

Hello UNIX experts, I have 124 text files in a directory. I want to extract the 45678th line of all the files sequentialy by file names. The extracted lines should be printed in the output file on seperate lines. e.g. The input Files are one.txt, two.txt, three.txt, four.txt The cat of four... (1 Reply)
Discussion started by: yogeshkumkar
1 Replies

9. Shell Programming and Scripting

Redirecting OUTPUT

Hi, I want to move the output of a command/script to a file as well as to to be displayed on stdout. Can anybody help me in this. Thanks in advace .. -Chanakya M (1 Reply)
Discussion started by: Chanakya.m
1 Replies

10. UNIX for Advanced & Expert Users

redirecting the output of aspell

Hi, I have 2 identical servers both running aspell but for some reason I can't redirect the output to a file on one of them. This is what I'm trying to do: echo feck | aspell -l > errors.txt On one machine this works fine but the other it doesn't (the file is created but it is empty). ... (6 Replies)
Discussion started by: leekb
6 Replies
Login or Register to Ask a Question