-1

I want to read file using a bash script and delete line(s) which are matching with my specific scenario (line(s) starting with 'z'). my code works fine if the 'inputFile' contains only alphabetic characters. but, if a line with 'specific characters of sed' (line eg : z-2.10.3.2 x/y/z F (&)[]+* ) then i got an error,(error : sed: -e expression #1, char 29: unterminated `y' command).

#!/bin/bash

inputFile="test.txt"

while IFS= read -r line
do
  echo "$line"
  if [[ $line == z* ]];
  then
    sed -i "/$line/d" $inputFile
  fi

done < "$inputFile"

i want to delete 'z-2.10.3.2 x/y/z F (&)[]+*' kind of lines, how can i do this...?

yasara malshan
  • 370
  • 3
  • 11
  • 1
    I recommend to do it either with `bash` or with `sed` but not with both. – Cyrus Jan 06 '19 at 08:25
  • `awk '!/^z/' file` or `grep -v '^z' file` or `sed -n '/^z/!p' file` – oguz ismail Jan 06 '19 at 08:25
  • 1
    ... or `sed '/^z/d' file`. – Cyrus Jan 06 '19 at 08:26
  • i want to do some task with that file line(s)., so i need to do it in bash.the main thing is , i need to delete some line(s) while reading the file line by line using that "$line".(key point) – yasara malshan Jan 06 '19 at 08:45
  • The second duplicate hints at avoiding the shell loop entirely, and combining all the commands into a single `sed` script. There are many duplicates which elaborate on tnis idea, using `sed` to create a `sed` script from a file of strings. I'll try to find a good third duplicate which shows exactly how to do that. – tripleee Jan 06 '19 at 09:10
  • Found one. The third duplicate is about replacing words, not deleting lines; but the adaptation should be straightforward enough. – tripleee Jan 06 '19 at 09:45

1 Answers1

0

As you mentioned you don't need line which has z*

Simply use grep -v

grep -vE "^[[:blank:]]*z" file 

I have created one scenario where I have a file which contains

root@ubuntu:~/T/e/s/t# cat file
hello world

sample line 1

sample line 2 world

sample line 3

sample line 4

In my case, I want to remove the line contains "world"

root@ubuntu:~/T/e/s/t# grep -v "world" file

sample line 1


sample line 3

sample line 4

If you want you can redirect your output in another file.

Mohit Rathore
  • 428
  • 3
  • 10
  • i want to do some task with that file line(s)., so i need to do it in bash.the main thing is , i need to delete some line(s) while reading the file line by line using that variable "$line".(key point) – yasara malshan Jan 06 '19 at 08:46
  • What kind of operation do you want to perform? provide more details. or you can just mention sample input and sample output. – Mohit Rathore Jan 06 '19 at 08:50
  • my actual purpose is compare two files and delete common lines from both files. (line numbers may be different, but line content is same in both files).i thought to do that using while loops in bash. is there a direct way...? – yasara malshan Jan 06 '19 at 09:06
  • 1
    try **diff t1 t2 | grep -E "<|>" | sed 's/<\|>//g'** I think this will resolve your problem. If you don't want to merge. you can grep "<" first and then ">" in two different commands. – Mohit Rathore Jan 06 '19 at 09:17
  • @yasaramalshan You should have stated your actual goal in your question instead of creating an [xy problem](http://xyproblem.info/). – Shawn Jan 06 '19 at 09:33
  • i need to delete common lines from that files, not a merge..and also, diff command is based on line numbers., isn't it..? (common lines won't be in same line number in both files) – yasara malshan Jan 06 '19 at 09:42