Shell script to check line end not ending with comma


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 2 Weeks Ago
Shell script to check line end not ending with comma

I have several line in a text file. for example
Code:
I like apple;
I like apple
I like orange;

Output: I like apple

I try to use
Code:
if grep -q "!\;$"; then

(Not work)
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules).

Last edited by Don Cragun; 2 Weeks Ago at 01:02 AM..
# 2  
Old 2 Weeks Ago
Quote:
Originally Posted by cmdcmd
I have several line in a text file. for example
Code:
I like apple;
I like apple
I like orange;

Output: I like apple

I try to use
Code:
if grep -q "!\;$"; then

(Not work)
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules).
If the output you're trying to get is:
Code:
I like apple

then using grep -q is never going to work for you. You'd want something more like:
Code:
grep -v ';$' filename

to do that. If you don't want grep to produce any output but an exit code telling you whether or not there were any lines in a file that did not have a semicolon as the last character on the line, then you want something more like:
Code:
if grep -qv ';$' filename
then	echo "At least one line in filename does not end in \";\"!"
else	echo "Every line in filename ends in \";\"."
fi


Last edited by Don Cragun; 2 Weeks Ago at 04:53 AM.. Reason: Fix typo: s/grep -q/grep -qv/
This User Gave Thanks to Don Cragun For This Post:
cmdcmd (2 Weeks Ago)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Check for empty line at end of a dynamic header in each file tpk UNIX for Dummies Questions & Answers 6 04-16-2015 03:54 AM
How can we remove comma from end of each line ? Litu19 Shell Programming and Scripting 4 05-20-2014 07:42 AM
Adding comma to end of each line if more than 1 line ATWC Shell Programming and Scripting 6 02-14-2014 02:22 AM
How to Remove comma as last character in end of last line of file? RahulJoshi Shell Programming and Scripting 6 02-25-2013 04:54 AM
How does a shell script recognize the end of a line? neelmani Shell Programming and Scripting 5 05-06-2012 12:46 PM
Adding comma at the end of every line jakSun8 UNIX for Dummies Questions & Answers 2 04-21-2012 08:42 PM
help needed with shell script to append to the end of a specific line in a file on multiple servers momin Shell Programming and Scripting 5 06-13-2011 05:15 AM
help on shell script to check line usyseng Shell Programming and Scripting 3 06-07-2011 06:17 AM
shell script to check blank line? usyseng Shell Programming and Scripting 0 06-07-2011 06:03 AM
Script to Check & Edit Content of a file (Addition of comma in each lines of code) big_nutz Shell Programming and Scripting 2 06-29-2010 07:26 AM
Help with shell script: moving end of line character udelalv Shell Programming and Scripting 8 11-10-2009 04:27 AM
Help on shell script : syntax error at line 62: `end of file' unexpected schandrakar1 Shell Programming and Scripting 7 12-12-2008 08:10 AM
check position of end of line(URGENT PLS) evvander Shell Programming and Scripting 6 11-11-2007 04:46 PM
check position of end of line for some specific lines senthil_is Shell Programming and Scripting 1 11-09-2007 12:19 AM
Add a comma at end of every line ST2000 Shell Programming and Scripting 4 07-14-2002 06:49 PM