Shell script to check line end not ending with comma


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to check line end not ending with comma
# 1  
Old 03-05-2019
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; 03-05-2019 at 02:02 AM..
# 2  
Old 03-05-2019
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; 03-05-2019 at 05:53 AM.. Reason: Fix typo: s/grep -q/grep -qv/
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can we remove comma from end of each line ?

Hi, How can we remove the comma from the end of each line. I have a csv file in below format. file.csv Name,age,gender,location, Joel,18,M,Newyork, Monoj,21,M,Japan, Litu,23,M,turki, Expected o/p file1.csv Name,age,gender,location (4 Replies)
Discussion started by: Litu19
4 Replies

2. Shell Programming and Scripting

Adding comma to end of each line if more than 1 line

I have a file with dates as '2013-01-01' '2013-01-02' I want the output to be '2013-01-01','2013-01-02' if there is only 1 entry then there should not be any comma. (6 Replies)
Discussion started by: ATWC
6 Replies

3. Shell Programming and Scripting

How to Remove comma as last character in end of last line of file?

how to Remove comma as last charector in end of last line of file: example: input file --------------- aaaaaa, bbbbbb, cccc, 12345, ____________ output file : ----------- aaaaaa, bbbbbb, (6 Replies)
Discussion started by: RahulJoshi
6 Replies

4. Shell Programming and Scripting

How does a shell script recognize the end of a line?

Hi friends , I want to know how does a shell script recognize the end of a line? . i have hunddres of proccedure to test where i want to ingnore the comments which starts with "--" .. it can start from the middle of the lines also. for example:: select * from table1; -- getting... (5 Replies)
Discussion started by: neelmani
5 Replies

5. UNIX for Dummies Questions & Answers

Adding comma at the end of every line

Hi all, I have this sample file (actual file is larger) and i need to add comma at the end of every line. 1234 4335 232345 1212 3535 Output 1234, 4335, 232345, 1212, 3535, TIA - jak (2 Replies)
Discussion started by: jakSun8
2 Replies

6. Shell Programming and Scripting

help needed with shell script to append to the end of a specific line in a file on multiple servers

Hi Folks, I was given a task to append three IP's at the end of a specific (and unique) line within a file on multiple servers. I was not able to do that with the help of a script. All I could was: for i in server1 server2 server3 server4 do ssh $i done I know 'sed' could be used to... (5 Replies)
Discussion started by: momin
5 Replies

7. Shell Programming and Scripting

help on shell script to check line

Hi Scripting Gurus, Can someone help to transform the below logic into a shell script, might be easy for some of you. I have a file with below text, I need if the line has the ":" and the above to it is not a blank line should print " <text>: is incorrect format" Apple: ... (3 Replies)
Discussion started by: usyseng
3 Replies

8. Shell Programming and Scripting

Help with shell script: moving end of line character

Hello. I have a file (old.txt) that I need to copy into another file (new.txt). Each line on old.txt ends with CR/LF but the position of CR/LF varies from one record to another. I need to copy each line of record to new.txt and move CR/LF in pos 165. Can I use awk to achieve this? How?... (8 Replies)
Discussion started by: udelalv
8 Replies

9. Shell Programming and Scripting

Help on shell script : syntax error at line 62: `end of file' unexpected

Hi All, I have written a korn script (code pasted below). It is giving the error while debugging "new.sh: syntax error at line 62: `end of file' unexpected". I have re-written the whole code in VI and explored all help related to this error on this Unix forum and tried it. Somehow, I could... (7 Replies)
Discussion started by: schandrakar1
7 Replies

10. Shell Programming and Scripting

Add a comma at end of every line

hello A small shell scripting help.. I have a file say with 5 lines of text (text file). At the end of everyline I need to add a comma at the end of the file. Thanks, ST2000 (4 Replies)
Discussion started by: ST2000
4 Replies
Login or Register to Ask a Question