For loop to get rid of first 2 lines(conditionally)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop to get rid of first 2 lines(conditionally)
# 8  
Old 02-26-2015
poor-man apprroach of in-place editing:
Code:
for f in file* ; do { rm $f; awk '/headers/{p=1;next}p' > $f'} < $f ; done

# 9  
Old 02-26-2015
Try also
Code:
awk '/headers/,EOF {print > FILENAME".res"}' file*

# 10  
Old 02-26-2015
If your first data line always starts with a date e.g.
Code:
26/2/2015 ...

then you can search this line and print up to the end.
Code:
ls -tr file* |
while read f
do
  sed -i -n '\m^ *[0-9]\{1,2\}/[0-9]\{1,2\}/[0-9]\{4\}m,$ p' "$f"
done

Or without GNU sed, the poor man approach
Code:
ls -tr file* |
while read f
do
  {
     rm -f "$f"
     sed -n '\m^ *[0-9]\{1,2\}/[0-9]\{1,2\}/[0-9]\{4\}m,$ p' > "$f"
  } < "$f"
done

# 11  
Old 02-26-2015
the below seem to work...could you please explain what you are doing ?

ls -tr file* |
while read f
do
sed -i -n '\m^ *[0-9]\{1,2\}/[0-9]\{1,2\}/[0-9]\{4\}m,$ p' "$f"
done
# 12  
Old 02-26-2015
Quote:
Originally Posted by crazy_max
okay that worked, sorry i was looking at wrong file i guess. didnt realized that it created a new file....

is there a way to do the same without creating a new file ?...just use the same file and also could you please explain what exactly is the below doing ?

'/headers/{p=1;next}p'
If the string headers is found then set variable p to 1 and proceed with the next cycle (line).
If p is not zero (or unset) then print the line (default action).
By switching the order one can save the next:
Code:
'p==1{print} /headers/{p=1}'

Or
Code:
'p==1; /headers/{p=1}'

Or
Code:
'p; /headers/{p=1}'

---------- Post updated at 05:00 PM ---------- Previous update was at 04:36 PM ----------

Quote:
Originally Posted by crazy_max
the below seem to work...could you please explain what you are doing ?

ls -tr file* |
while read f
do
sed -i -n '\m^ *[0-9]\{1,2\}/[0-9]\{1,2\}/[0-9]\{4\}m,$ p' "$f"
done
Code:
ls -tr file* |
while read f
  ...
done

processes the oldest files first, like in your initial post.
The file loop is an alternative to a for loop.
sed:
-i in place replacement
-n noprint mode, so it needs explicit printing.
/search/,$ p from the line where search is found thru the $=last line print.
search is a RE (regular expression), here ^ *[0-9]\{1,2\}/[0-9]\{1,2\}/[0-9]\{4\}. Because the expression contains a / that collides with the / delimiter, I have replaced the delimiters to another character m that is not in the RE\msearchm.
The RE means: beginning of line, optional spaces, a digit 1 or 2 times, a /, a digit 1 or 2 times, a /, a digit 4 times.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Getting Rid of Annoying Bootstrap Table Borders and Wayward Table Lines

Bootstrap is great; but we have had some issues with Bootstrapped <tables> (and legacy <fieldset> elements) showing annoying, wayward lines. I solved that problem today with this simple jQuery in the footer: <script> $(function(){ $('tr, td, fieldset,... (0 Replies)
Discussion started by: Neo
0 Replies

2. Shell Programming and Scripting

Picking up files conditionally

Hi I have a scenario: I have a directory say DIR1 (no sub directories) and have few files in that directory as given below: app-cnd-imp-20150820.txt app-cxyzm-imp-20150820.txt app-petco-imp-20150820.txt app-mobility-imp-20150820.txt app-mobility-imp-20150821.txt... (7 Replies)
Discussion started by: Saanvi1
7 Replies

3. Shell Programming and Scripting

get rid of unwanted lines

Hi , My output contains the below contents When i execute df -h | grep / | grep -v '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' i get output as below tpshghghg.fgh.com:/vol/scmvol1/scm-vobstore/WebArch.vbs 1.7T 1.3T 452G 74% /vob/WebArch... (6 Replies)
Discussion started by: ptappeta
6 Replies

4. Shell Programming and Scripting

Reading two lines in a while loop and combining the lines

Dear all, I have a file like this: imput scaffold_0 1 scaffold_0 10000 scaffold_0 20000 scaffold_0 25000 scaffold_1 1 scaffold_1 10000 scaffold_1 20000 scaffold_1 23283 and I want the output like this: scaffold_0 1 scaffold_0 10000 scaffold_0 10000 scaffold_0 20000... (6 Replies)
Discussion started by: valente
6 Replies

5. Shell Programming and Scripting

sed to conditionally delete multiple lines

Hello I'd like to remove any line in an output file that is preceded by one or more warning messages (each warning is on a separate line). Example : WARNING: Estimation of lower confidence limit of \rho failed; setting it to 0. 822 28447 ... (4 Replies)
Discussion started by: jossojjos
4 Replies

6. Shell Programming and Scripting

Loop through file to sum conditionally

Hi, I have a file with header, detail and trailer records. HDR|111 DTL|abc|100|xyz DTL|abc|50|xyz TRL|150 I need to add the values in 3rd field from DTL records. Using awk, I am doing it as follows: awk -F'|' '$1=="DTL"{a += $3} END {print a}' <source_file> However, I want to... (3 Replies)
Discussion started by: delta21
3 Replies

7. Shell Programming and Scripting

Conditionally delete last X lines

delete last X lines,which start with + example file: test1 test2 remove1 remove2 one liner shell is preferred. (8 Replies)
Discussion started by: honglus
8 Replies

8. Shell Programming and Scripting

How to get different lines using a for loop

Hi I am trying to write a small AWK program to extract 324 lines which have a pattern in the line numbers. For examples, I want to extract lines 2, 5, 7, 11 and lines like (2+662), (5+662), (7+662), (11+662) and (2+662+662), (5+662+662), (7+662+662), (11+662+662) and so on for 27 times from... (5 Replies)
Discussion started by: ananyob
5 Replies

9. UNIX for Dummies Questions & Answers

Conditionally joining lines in vi

I've done this before but I can't remember how. Too long away from vi. I want to do a search are replace, but I want the replace to be a join. Example see spot run see spot walk see spot run fast see spot hop %s/run$/<somehow perform a join with the next line>/g so the results... (0 Replies)
Discussion started by: ifermon
0 Replies

10. Shell Programming and Scripting

Get rid of first & last lines from STD_OUT

I have an application that executes, printing a bunch of text to the screen... only problem is that I don't want to see the first line (the internal command being run) or the last line (it says "done") is there something to chop off the first and last line simply through use of piping the... (2 Replies)
Discussion started by: jjinno
2 Replies
Login or Register to Ask a Question