Appending a word to the last line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending a word to the last line
# 1  
Old 08-09-2011
Appending a word to the last line

Hi,

I would like to append input given id at last line of file.
For ex: In the following sample.txt file i would like to append the input given user id (after id6,id7) but it is adding on the next line instead same line.

Sample.txt
read=id1,id2,id3
write=id4,id5,id6

Quote:
#!/usr/bin/ksh
echo "Enter id"
read id
echo ","{$id} >> sample.txt
Thanks
Raveendran
# 2  
Old 08-09-2011
GNU sed:
Code:
#!/usr/bin/ksh
echo "Enter id"
read id
sed -i '$ s/$/,'"$id"'/' sample.txt

Without "-i" option you can redirect output to /tmp/tempfile.$$ and then mv this file to "sample.txt".

Last edited by yazu; 08-09-2011 at 12:42 AM..
This User Gave Thanks to yazu For This Post:
# 3  
Old 08-09-2011
Code:
printf ,%s\\n "$id" >> file
printf %s\\n -,.j w q | ed -s file

Regards and welcome to the forum,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 08-09-2011
alister,

Quote:
Originally Posted by alister
Code:
printf ,%s\\n "$id" >> file
printf %s\\n -,.j w q | ed -s file

Regards and welcome to the forum,
Alister
can you please explain that command?

Thank You,
Ahamed

---------- Post updated at 11:50 PM ---------- Previous update was at 11:46 PM ----------

Never mind, I got it... Smilie

-,.j
w
q

these are the input to the "ed"

Thanks,
Ahamed
# 5  
Old 08-09-2011
simply use this :
Code:
sed 's/$/,'"$ID"'/' filename


Last edited by Franklin52; 08-09-2011 at 11:04 AM.. Reason: Please use code tags for data and code samples, thank you
This User Gave Thanks to Mani_apr08 For This Post:
# 6  
Old 08-10-2011
Thanks alot

Thanks alot . Sorry for the late reply.
# 7  
Old 08-10-2011
raveendran...Sorry for jumping onto your post, but i am stuck in my statement.can you please have a look @my post? thx 2u.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

2. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

3. Shell Programming and Scripting

Appending lines with word frequencies, ordering and indexing a column

Dear All, I have the following input data: w1 20 g1 w1 10 g1 w2 12 g1 w2 23 g1 w3 10 g1 w3 17 g1 w3 12.5 g1 w3 21 g1 w4 11 g1 w4 13.2 g1 w4 23 g1 w4 18 g1 First I seek to find the word frequencies in col1 and sort col2 in ascending order for each change in a col1 word. Second,... (5 Replies)
Discussion started by: Ghetz
5 Replies

4. Shell Programming and Scripting

Appending the first word of each line to the end of each line

Hi Experts, Am relatively new to shell programming so would appreciate some help in this regard. I am looking at reading from a file, line by line, picking the first word of each line and appending it to the end of the line. Any suggestions? INPUT FILE - 3735051 :... (7 Replies)
Discussion started by: hj007
7 Replies

5. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

6. UNIX for Dummies Questions & Answers

regular expression for replacing the fist word with a last word in line

I have a File with the below contents File1 I have no prior experience in unix. I have just started to work in unix. My experience in unix is 0. My Total It exp is 3 yrs. I need to replace the first word in each line with the last word for example unix have no prior experience in... (2 Replies)
Discussion started by: kri_swami
2 Replies

7. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

8. Shell Programming and Scripting

sed: appending alternate line after previous line

Hi all, I have to append every alternate line after its previous line. For example if my file has following contents line 1: unix is an OS line 2: it is open source line 3: it supports shell programming line 4: we can write shell scripts Required output should be line1: unix is an OS it is... (4 Replies)
Discussion started by: rish_max
4 Replies

9. Shell Programming and Scripting

Appending the line number and a seperator to each line of a file ?

Hi, I am a newb as far as shell scripting and SED goes so bear with me on this one. I want to basically append to each line in a file a delimiter character and the line's line number e.g Change the file from :- aaaaaa bbbbbb cccccc to:- aaaaaa;1 bbbbbb;2 cccccc;3 I have worked... (4 Replies)
Discussion started by: pjcwhite
4 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question