Delete lines from file using Unix Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete lines from file using Unix Script
# 1  
Old 06-02-2010
Delete lines from file using Unix Script

Hi Experts,

I have a file in the below given format. First two lines are header and Trailer. Rest all are transaction Lines. I have to delete all other lines except first line (Header) and lines which contains 5000 in 1st column and 0 in 5th column.

Can anyone please kindly provide me with the script code. Thanks in Advance.



0000 00000 Header
0000 00000 Trailer
1000 00000 Others.
9999 00000
5000 09999 00000008499 0000999 0
5000 09999 00000008499 0000999 7
5000 09999 00000008499 0000999 0
5000 09999 00000008499 0000999 0
5000 09999 00000008499 0000999 7
4300 09999 00000008499 0000999 0
6000 09999 00000008499 0000999 0
1000 09999 00000008499 0000999 7
# 2  
Old 06-02-2010
Hi,
Probably you can try this one.

Code:
awk ' { if ( $0~/Header/ || $1 == 5000 || $5 == 0 ) { print } }' filename > new

mv new filename

Regards
Naree

Last edited by Franklin52; 06-02-2010 at 02:54 AM.. Reason: Please use code tags, thank you
This User Gave Thanks to naree For This Post:
# 3  
Old 06-02-2010
Quote:
Originally Posted by naree
Hi,
Probably you can try this one.

Code:
awk ' { if ( $0~/Header/ || $1 == 5000 && $5 == 0 ) { print } }' filename > new

mv new filename

Regards
Naree


---------- Post updated at 12:37 ---------- Previous update was at 12:36 ----------


If your header line will not always contain the literal word Header, then the previous solution will not meet your requirement and you will want to check this sed approach:
Code:
sed q file > tmpfile
sed '/^5000.* 0$/!d' file >> tmpfile
mv tmpfile file

# 4  
Old 06-02-2010
A tad shorter:
Code:
awk '/Header/ || $1==5000 && $5==0' infile

# 5  
Old 06-11-2010
Hi All,

Thank you very much for the replies. I request few changes. Please click the below link to see the sample file which needs to be modified using the script.

SampleFile on Flickr - Photo Sharing!

Requirements:

1. Unix script should modify the file placed in /usr/sap/xyz folder.
2. First and Second lines with 0000 are Header and Trailer. Unix Script should prefix H before 1st 0000 and T before second 0000.
3. Script should remove all lines other than which has value 5000 in 1st column and 0 in 5th column.


File Snippet Before Execution of Script:

0000 00000 00000008499 0000999 0
0000 00000 00000008499 0000999 0
1000 00000 00000008499 0000999 0
9999 00000 00000008499 0000999 0
5000 09999 00000008499 0000999 0
5000 09999 00000008499 0000999 7
5000 09999 00000008499 0000999 0
5000 09999 00000008499 0000999 0
5000 09999 00000008499 0000999 7
4300 09999 00000008499 0000999 0
6000 09999 00000008499 0000999 0
1000 09999 00000008499 0000999 7



File Snippet After Execution of Script:-

H0000 00000 00000008499 0000999 0
T0000 00000 00000008499 0000999 0
5000 09999 00000008499 0000999 0
5000
09999 00000008499 0000999 0
5000
09999 00000008499 0000999 0

Last edited by phani333; 06-11-2010 at 11:02 PM..
# 6  
Old 06-11-2010
Code:
awk 'BEGIN {a=1}
      $1=="0000"&&a==1 {a++;print "H" $0;next}
      $1=="0000"&&a==2 {a=1;print "T" $0;next}
      $1=="5000" && $NF=="0" {print $0}
     ' urfile

But I don't understand how you get the 4 and 8 in the output.

Code:
H0000 00000 00000008499 0000999 4
T0000 00000 00000008499 0000999 8

This User Gave Thanks to rdcwayx For This Post:
# 7  
Old 06-12-2010
Hi,

Apologies. there is no 4 and 8. its mistakenly typed. My requirement is to Append H and T before line 1 and 2 and remove all other lines other than one which has 5000 and 0 in 1st and 5th column.

Thanks,
Phani Akella.

---------- Post updated 06-12-10 at 10:27 AM ---------- Previous update was 06-11-10 at 12:26 PM ----------

Hi rdcwayx,

I have modified your code and it is working fine now. Thank you very much. I have changed NF with $5.

Code:

#!/bin/sh

for file in /sapmnt/XD5/SAPPI/Pcard/Outbound/*
do

awk 'BEGIN {a=1}
$1=="0000"&&a==1 {a++;print "H" $0;next}
$1=="0000"&&a==2 {a=1;print "T" $0;next}
$1=="5000"&&$5=="0" {print $0}
' "$file" > tempfile

mv tempfile "$file"
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

2. Shell Programming and Scripting

Help with UNIX script --Read from one file and delete entries in other

Hi Guru's The script has to read an entry from one file and delete the set of lines form other file. Below is the format of the file. In the below example, script should read the entries from input file 2 and delete the entries from input file 1. Input file 1 cn: test@test1.com abc:... (7 Replies)
Discussion started by: Samingla
7 Replies

3. Shell Programming and Scripting

How to delete the lines from file using script?

Hi Iam having file like below 10.238.52.65 pun-ras-bng-mhs-01 server 10.238.52.65 pun-ras-bng-mhs-01 10.10.10.10 10.238.52.65 pun-ras-bng-mhs-01 10.10.20.10 10.238.54.1 enk-ras-bng-cse-01 server 10.238.54.1 enk-ras-bng-cse-01 10.10.30.10 10.238.54.1 enk-ras-bng-cse-01 10.10.10.10 ... (5 Replies)
Discussion started by: surender reddy
5 Replies

4. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

5. Shell Programming and Scripting

script to delete lines from a txt file if pattern matches

File 6 dbnawldb010-b office Memphis_Corp_SQL_Diff Memphis-Corp-SQL-Inc-Application-Backup 03/09/11 03:24:04 42 luigi-b IPNRemitDB Memphis_Corp_SQL_Diff Memphis-Corp-SQL-Inc-Application-Backup 03/10/11 00:41:36 6 ebs-sqldev1-b IPNTracking Memphis_Corp_SQL_Diff... (4 Replies)
Discussion started by: ajiwww
4 Replies

6. Shell Programming and Scripting

looking for a script that will delete lines in a text file

it will grep for a line and then delete these line. how do i begin to write this script if theres no available one? (3 Replies)
Discussion started by: garfish
3 Replies

7. Shell Programming and Scripting

HP Unix Script to Delete the lines in a file

Hi Experts, I have a file format as mentioned below. I would like to have unix script (HP Unix) which can: 1. Remove first 6 and last 3 lines. 2. Delete the lines where 3rd column having Alpha Numeric Number 3. Delete the lines where 4th column having 0.00 4. Calculate the sum of all the... (16 Replies)
Discussion started by: phani333
16 Replies

8. Shell Programming and Scripting

Need Shell Script to delete lines in a file

Hi All, I have a file with 3 columns (Bank Name, Account Number and Amount). My requirement, I need to delete lines using Unix Shell script: 1. Which are having Alphanumeric characters in Account Number (eg. Line3). 2. Which are having 0.00 in amount. (eg. Line4) 3. And also I need to... (4 Replies)
Discussion started by: phani333
4 Replies

9. Shell Programming and Scripting

Delete lines at several places in a file with script

Hi, I am a newbie to shell scripting, and I have a file which quite large which I would like to delete lines at certain places. I want to search for a keyword which is recurring in the file. When matched I would like to delete the line. And when the file was so huge I thought I ought to learn... (3 Replies)
Discussion started by: mr_andrew
3 Replies

10. Shell Programming and Scripting

How to Delete all lines in a file from a script

I am trying to delete all the lines out a file from a unix script. Please help Platform is Sun (3 Replies)
Discussion started by: alnita
3 Replies
Login or Register to Ask a Question