Conditionally add character at end of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditionally add character at end of line
# 8  
Old 10-30-2014
Try egrep.

@cero: That's removing the last char!
This User Gave Thanks to RudiC For This Post:
# 9  
Old 10-30-2014
@Rudi
thanks for the check - not my day today (didn't get enough sleep)...
# 10  
Old 10-30-2014
RudiC,
When I try:
Code:
sed -E 's/(;|.)$/;/' MyFile.csv

it replaces the last character on every line with a semicolon instead of adding a semicolon to the end of lines that don't already have one.

Try:
Code:
sed '/[^;]$/s/$/;/' MyFile.csv

These 3 Users Gave Thanks to Don Cragun For This Post:
# 11  
Old 10-30-2014
Longhand using builtins only, OSX 10.7.5, default shell and terminal...
Code:
#!/bin/sh
# add_char.sh
> /tmp/text
> /tmp/txt
echo 'web9331801;01/01/2014 23:39:35;;"93962";353150256;
web9331802;01/01/2014 23:44:29;;"479288";353153538;
web9331803;01/01/2014 00:00:11;;"877219";352215572
web9331805;01/01/2014 00:00:11;;"877219";352215572
web9331807;01/01/2014 00:00:14;;"624588";352215598
web9331801;01/01/2014 23:39:35;;"93962";353150256;
web9331802;01/01/2014 23:44:29;;"479288";353153538;
web9331803;01/01/2014 00:00:11;;"877219";352215572
web9331805;01/01/2014 00:00:11;;"877219";352215572
web9331807;01/01/2014 00:00:14;;"624588";352215598
web9331801;01/01/2014 23:39:35;;"93962";353150256;
web9331802;01/01/2014 23:44:29;;"479288";353153538;
web9331803;01/01/2014 00:00:11;;"877219";352215572
web9331805;01/01/2014 00:00:11;;"877219";352215572
web9331807;01/01/2014 00:00:14;;"624588";352215598
web9331801;01/01/2014 23:39:35;;"93962";353150256;
web9331802;01/01/2014 23:44:29;;"479288";353153538;
web9331803;01/01/2014 00:00:11;;"877219";352215572
web9331805;01/01/2014 00:00:11;;"877219";352215572
web9331807;01/01/2014 00:00:14;;"624588";352215598' > /tmp/text
while read line
do
	if [ "${line:$((${#line}-1)):1}" == ";" ]
	then
		printf "$line\n" >> /tmp/txt
	else
		printf "$line"';'"\n" >> /tmp/txt
	fi
done < /tmp/text
cat /tmp/txt

Results...
Code:
Last login: Thu Oct 30 19:10:57 on ttys000
AMIGA:barrywalker~> cd ~/Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 add_char.sh
AMIGA:barrywalker~/Desktop/Code/Shell> ./add_char.sh
web9331801;01/01/2014 23:39:35;;"93962";353150256;
web9331802;01/01/2014 23:44:29;;"479288";353153538;
web9331803;01/01/2014 00:00:11;;"877219";352215572;
web9331805;01/01/2014 00:00:11;;"877219";352215572;
web9331807;01/01/2014 00:00:14;;"624588";352215598;
web9331801;01/01/2014 23:39:35;;"93962";353150256;
web9331802;01/01/2014 23:44:29;;"479288";353153538;
web9331803;01/01/2014 00:00:11;;"877219";352215572;
web9331805;01/01/2014 00:00:11;;"877219";352215572;
web9331807;01/01/2014 00:00:14;;"624588";352215598;
web9331801;01/01/2014 23:39:35;;"93962";353150256;
web9331802;01/01/2014 23:44:29;;"479288";353153538;
web9331803;01/01/2014 00:00:11;;"877219";352215572;
web9331805;01/01/2014 00:00:11;;"877219";352215572;
web9331807;01/01/2014 00:00:14;;"624588";352215598;
web9331801;01/01/2014 23:39:35;;"93962";353150256;
web9331802;01/01/2014 23:44:29;;"479288";353153538;
web9331803;01/01/2014 00:00:11;;"877219";352215572;
web9331805;01/01/2014 00:00:11;;"877219";352215572;
web9331807;01/01/2014 00:00:14;;"624588";352215598;
AMIGA:barrywalker~/Desktop/Code/Shell> _

# 12  
Old 10-30-2014
More variations
Code:
sed 's/;\{0,1\}$/;/' file

GNU sed -r / BSD sed -E:
Code:
sed -E 's/;?$/;/' file

Code:
sed 's/[^;]$/&;/' file

--
For a fixed number of fields:
Code:
awk -v n=6 '{$n=x}1' FS=\; OFS=\; file


Last edited by Scrutinizer; 10-30-2014 at 05:15 PM..
# 13  
Old 10-30-2014
Quote:
Originally Posted by wisecracker
Longhand using builtins only, OSX 10.7.5, default shell and terminal...
... ... ...
Code:
while read line
do
	if [ "${line:$((${#line}-1)):1}" == ";" ]
	then
		printf "$line\n" >> /tmp/txt
	else
		printf "$line"';'"\n" >> /tmp/txt
	fi
done < /tmp/text

... ... ...
This happens to work with the supplied sample data (since it does not contain any % or \ characters, but it is very dangerous to use a user-supplied string as a format operand to printf.

This would be much safer:
Code:
while read -r line
do	if [ "${line:$((${#line}-1)):1}" == ";" ]
	then	printf '%s\n' "$line"
	else	printf '%s;\n' "$line"
	fi
done < /tmp/text > /tmp/txt

(Note also the addition of the -r option to read.)

Last edited by Don Cragun; 10-30-2014 at 04:43 PM.. Reason: Simplify output redirection.
This User Gave Thanks to Don Cragun For This Post:
# 14  
Old 10-30-2014
And here's the inversed corollary of Don Cragun's code...Smilie
Code:
sed '/;$/!s/$/;/' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add a character at end of line?

Hai, I have got a small requirement in my script. and i am using bash shell. I need to add a dot (.) for some particular line in a file. Say for example, $Cat rmfile 1 This is line1 2 This is line2 3 This is line3 O/p should be : $Cat rmfile 1 This is line1 2 This is line2. #... (2 Replies)
Discussion started by: Sivajee
2 Replies

2. Shell Programming and Scripting

How to remove new line character at end of file.

I need to remove new line character from end of file. Suppose here are content. a|b|c|d|r a|b|c|d|r a|b|c|d|r <new line> that means file contains 4 lines but data is there in 3 lines. so I want that only 3 lines should be there in file. Please help (20 Replies)
Discussion started by: varun940
20 Replies

3. Shell Programming and Scripting

Add text at the end of line conditionally

Hi All, I have a file as below: cat myfile abcdef NA rwer tyujkl na I wish to add the text ".txt" at the end of all lines except the lines starting with NA or na. I know i can add text at the end of line using following command but I am not sure how to valiate the condition. (14 Replies)
Discussion started by: angshuman
14 Replies

4. Shell Programming and Scripting

add character to every end-of line in file

Hi All I have a file which conatins record.the length of every records is 47. problem : in the end of record i don't have a "\015" character. i want to add this "\015" charcter in the end of every record. the file contains something like 700 records. i've tried with sed command - nothing. ... (8 Replies)
Discussion started by: naamas03
8 Replies

5. Shell Programming and Scripting

add character to the end of each line in file

hi all i have 32 lines in file. the length of each line is 82 , i want that in the end of each line , means in postion 83-84 to put two characters 0d(=\015), 0a(=\012) i want that the 0d will be in postion 83 and the 0a will be in postion 84 in each line of the file how shall i do it ? ... (7 Replies)
Discussion started by: naamas03
7 Replies

6. Shell Programming and Scripting

Adding a special character at the end of the line

I used following to add * at the end of the line in file1. It adds * at the end but has a space before it for some lines but some other lines it adds exactly after the last character. How do I take out the space ? sed 's/$/*/' file1 > file2 example: contents of file1 : ... (2 Replies)
Discussion started by: pitagi
2 Replies

7. UNIX for Dummies Questions & Answers

How to add new line character at the end of a file

hi all, i have this question: How to add new line character at the end of a file???? i need this because i am loading a file to sybase and i have problems with the last record thanks for your help (5 Replies)
Discussion started by: DebianJ
5 Replies

8. UNIX for Dummies Questions & Answers

stripping last character from end of each line

Hi, sorry for being dumb but I have a file with a variable amount of records (day to day it differs) but is fixed at 80 characters wide. Unfortunately the 80th and last charachter in each line is a "^M" carriage return character which i want to get rid of. Is there a really easy command that i can... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

9. Shell Programming and Scripting

end of the line character

How do i determine what the end of the line character is in a text file. for eg. is it \n or \f or \t etc.. Is there a unix command for this? (5 Replies)
Discussion started by: zomboo
5 Replies

10. UNIX for Dummies Questions & Answers

End of line character in BAsh

I'm trying to match the end of line character in a shell script under bash. What I have done it got is a loop that reads each line of a file into a temp variable and then I get each letter of that variable and do some work on it. One think I want to do it check if the character I checking if the... (1 Reply)
Discussion started by: Rukshan
1 Replies
Login or Register to Ask a Question