Deleting selected lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting selected lines in a file
# 1  
Old 07-08-2014
Deleting selected lines in a file

Hi Guys ,

I have two files say a1 and a2 having following contents

a1
Code:
dag
wfd

a2
Code:
dag
wfd
chire
hcm

I want to delete only the lines in a2 which are in a1 and final output of a2 should be

a2
Code:
chire
hcm

Wrote the follwing script but I think making a small mistake .

Code:
#! /bin/bash
IFS=$'\r\n'
XYZ=($(cat a1))
for row in ${XYZ[@]};
do
sed -i '/"$row"/d' a2 > tmpfile
mv tmpfile a2
done

Please help.

Thanks
Pradeep

Last edited by Scrutinizer; 07-08-2014 at 09:24 AM.. Reason: icode tags -> code tags plus additional code tags for data samples......
# 2  
Old 07-08-2014
I would try grep for that:

Code:
grep -vxFf a1 a2 > a3

Why is there a carriage return in your IFS. Are the files in DOS format? If so, convert to unix format first (tr -d '\r' file.dos > file.ux)..

Last edited by Scrutinizer; 07-08-2014 at 09:46 AM..
# 3  
Old 07-08-2014
Code:
awk 'NR == FNR {A[$0]; next} {if(!($0 in A)) print}' a1 a2 > tmpfile && mv tmpfile a2

# 4  
Old 07-08-2014
Thanks a lot Scrutinizer and Srini.Both the scripts work but possible to make my code work with any modification ??just curious..
# 5  
Old 07-08-2014
Regarding your approach,
  • you could limit IFS to $'\n'.
  • for row in ${XYZ[@]}; would need to be for row in "${XYZ[@]"};
You do not need sed and the -i option is also disrupting things.

You could do this

Code:
#! /bin/bash
IFS=$'\n'
XYZ=($(<a1))
while read -r line
do
  for row in "${XYZ[@]}"
  do
    if [[ "$row" == "$line" ]]; then
      continue 2 
    fi
  done
  printf "%s\n" "$line"
done < a2


With bash 4 you could do something like this (using an associative array):
Code:
#! /bin/bash4
declare -A XYZ

while read -r line
do
  XYZ[$line]=x
done < a1

while read -r line
do
  if [[ ${XYZ[$line]} != x ]]; then
    printf "%s\n" "$line"
  fi
done < a2


Last edited by Scrutinizer; 07-08-2014 at 01:22 PM..
# 6  
Old 07-08-2014
Correction:
Code:
for row in ${XYZ[@]};
do
        sed -i "/$row/d" a2
done

Note: variable replacement does not happen inside single quote or strong quote. Also no need to redirect output to another file if you are using -i option.
# 7  
Old 07-08-2014
Thanks a lot Scrutinizer and Yoda..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selected lines from a file based on another file

Hello, I am using Awk in Ubuntu 12.04 First file: I have a file like this: SNP1 1 198.2 SNP2 1 124.5 SNP3 1 124.4 . . . Second file: I have another file like this: SNP2 SNP5 SNP10 . . . I want to create a third file like my first file but keeping ONLY the SNPs that... (8 Replies)
Discussion started by: Homa
8 Replies

2. Shell Programming and Scripting

Reading selected lines from a param file

Hi all, I have a question for the Gurus. I apologize if this has bee shared before but I couldn't find the link. I am trying to read parameters from an external parameter file. What I m trying to achieve is read selected lines from an external parameter file into the script. for eg my param... (4 Replies)
Discussion started by: maverick1947
4 Replies

3. Shell Programming and Scripting

Print selected lines from file in order

I need to extract selected lines from a log file, I can use grep to pull one line matching 'x' or matching 'y', how can I run through the log printing both matching lines in order top to bottom. i.e line 1 xyz - not needed line 2 User01 - needed line 3 123 - not needed line 4 Info - needed... (2 Replies)
Discussion started by: rosslm
2 Replies

4. Shell Programming and Scripting

trying to print selected fields of selected lines by AWK

I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by awk 'NR==29,NR==10029' File1 > File2 and then doing awk '{print $1, $2, $13, $14}' File2 > File3 Can... (3 Replies)
Discussion started by: ananyob
3 Replies

5. Shell Programming and Scripting

deleting lines from file

We have a server that logs transactions to a file. I want to write a script that will delete the first 50 lines of the file daily without renameing the file or moving the file. (8 Replies)
Discussion started by: daveisme
8 Replies

6. UNIX for Advanced & Expert Users

Deleting lines from a file

How I can delete 100 lines anywhere in a file without opening a file and without renaming the file. (11 Replies)
Discussion started by: Nirgude07
11 Replies

7. UNIX for Dummies Questions & Answers

Deleting whole lines from a file

I have a file with 65 sets of 35 coordinates, and would like to isolate these coordinates so that I can easily copy the coordinates to another file. The problem is, I've got a 9 line header before each set of coordinates (so each set is 44 lines long). There are a zillion threads out there about... (3 Replies)
Discussion started by: red baron
3 Replies

8. Shell Programming and Scripting

Deleting lines in a file

How do I delete all the lines after the line containing text ***DISCLOSURES*** . I want to delete this line too. Thank you (2 Replies)
Discussion started by: reachsamir
2 Replies

9. Shell Programming and Scripting

Deleting last 2 lines from the file.

Hi I have a file & always I need to remove or delete last 2 lines from that file. So in a file if I have 10 lines then it should return me first 8 lines. Can someone help me? (4 Replies)
Discussion started by: videsh77
4 Replies

10. Shell Programming and Scripting

Deleting Lines from .csv file

Hello All, I have a .csv file and I have to delete the selcted records stored in a vairable e.g echo $lname 7 88 91 94 97 100 103 106 I dont know how to pass the variable name to "sed" for deleting the $lname from a file can any one help as this is very urgent. $lname is changing the... (3 Replies)
Discussion started by: 009satya
3 Replies
Login or Register to Ask a Question