To append last word of the line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers To append last word of the line
# 1  
Old 10-22-2013
To append last word of the line

Hi All,
I need to append the last word of a line to the same line inside a file.
For example::
Below are the contents inside an input file
Code:
A/B/C
D/E/F

Output file should be::

Code:
A/B/C/C
D/E/F/F

# 2  
Old 10-22-2013
Try:
Code:
awk -F/ '$(NF+1)=$NF' OFS=/ file

# 3  
Old 10-22-2013
Code:
sed 's:.*\(/.\):&\1:' file

# 4  
Old 10-22-2013
Thank Franklin, Bala.

Franklink,
It would be great if you can give a brief explaination.
Regards,
# 5  
Old 10-22-2013
Quote:
Originally Posted by gujrathinr
Thank Franklin, Bala.

Franklink,
It would be great if you can give a brief explaination.
Regards,
$(NF+1)=$NF creates a new column ($NF+1) with the value of the last column $NF.

The statement $(NF+1)=$NF is a condition without braces. Since the condition is true, awk prints the current record per default.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to append word at end of line

hello Team, I am looking for sed command or script which will append word at end of line. for example. I want to validate particular filesystem with mount |<filesystem name> command. if nodev parameter is not there then it should add in the fstab file with receptive to the filesystem. # mount... (8 Replies)
Discussion started by: ghpradeep
8 Replies

2. 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

3. 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

4. Shell Programming and Scripting

How to append word count at end of file?

Hi guys, I have to append the word count of a file at the end of the file name like this - > "filename_2345" where 2345 is the word count of "filename". How do i achieve this using one command ? I am not allowed to store the word count in a variable and then concatenate. Request your... (1 Reply)
Discussion started by: peter2312
1 Replies

5. Shell Programming and Scripting

How to append something to a word using sed command

Hi, How to append something to already existing word. Suppose, I have the following line as a part of a file. VVV= jdbc:... (6 Replies)
Discussion started by: Dpu
6 Replies

6. 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

7. 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

8. Shell Programming and Scripting

Sed : identify a pattern and append a word at the end of a line

Hello to all, On aix, I want to identify a term on a line in a file and then add a word at the end of the line identified. I do not want the word to be added when the line contains the symbol "#". I use the following command, but it deletes the term identified then adds the word. #sed... (4 Replies)
Discussion started by: dantares
4 Replies

9. 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

10. UNIX for Dummies Questions & Answers

append word to lines

Hi all, Can someone suggest how to append some word to all lines in file. for example word "Honey" to file f1 with lines: Mia Katrin Elizabeth to get Honey Mia Honey Katrin Honey Elizabeth Thanks in advance Givi (3 Replies)
Discussion started by: giviut
3 Replies
Login or Register to Ask a Question