Visit Our UNIX and Linux User Community


Adding new lines to a file + adding suffix to a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding new lines to a file + adding suffix to a pattern
# 1  
Old 06-11-2010
Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern.

Ok I have a file:
Code:
#cat names.txt

name: John Doe
stationed: 1

name: Michael Sweets
stationed: 41

.
.
.

And would like to change it to:
Code:
name: John Doe
employed
permanently
stationed: 1-office

name: Michael Sweets
employed
permanently
stationed: 41-office

Lines "employed" and "permanently" are static and I would like to add those two lines after every name. Then I would like to add a suffix "-office" after every number.

Can anyone please give me a solution.
thanks in advance.

HemoLymph

Last edited by Franklin52; 06-11-2010 at 07:00 AM.. Reason: Please use code tags!
# 2  
Old 06-11-2010
try this:
Code:
awk -F: ' { if ($1=="name") { printf "%s%s\n%s\n%s\n", "name:", $2, "employed", "permanently"} \
else if($0!="") printf "%s%s\n\n", $0, "-office"}  ' names.txt

# 3  
Old 06-11-2010
Try,

Code:
awk '/^name/ { print $0; print "employed\npermanently" } /^stationed/ { print $0"-office\n" }' inputfile

# 4  
Old 06-11-2010
Code:
$> awk '/^name:/ {print $0 RS "employed" RS "permanently"; next} /^stat/ {print $0"-office"; next}1' infile
name: John Doe
employed
permanently
stationed: 1-office

name: Michael Sweets
employed
permanently
stationed: 41-office

# 5  
Old 06-11-2010
Try this:
Code:
awk -F": " '
/name/{s=$0 RS "employed" RS "permanently" RS}
/stationed/{print s $0 "-office\n"}' file

# 6  
Old 06-11-2010
With sed:

Code:
sed '/name:/,/^$/ {
s/\(name:.*\)$/\1 \
employed \
permanently/
/stationed: / s/\([0-9]*\)$/\1-office/
}' names.txt

# 7  
Old 06-11-2010
Thanks for all the help.
Kind regards.

Hemolymph

Previous Thread | Next Thread
Test Your Knowledge in Computers #423
Difficulty: Medium
C# is a general-purpose, multi-paradigm programming language encompassing strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding to an array in an external file, and adding elements to it.

I have an array in an external file, "array.txt", which contains: char *testarray={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};I want to be able to add an element to this array, and have that element display, whenever I call it, without having to recompile... (29 Replies)
Discussion started by: ignatius
29 Replies

2. Shell Programming and Scripting

Adding lines to a large file

Hello, I have a relatively large text file (25,000K) consisting of records of data. For each record, I need to create a new line based on what is already there. Every record has a block that looks like, M END > <ID> 1 > <SOURCE> KEGG > <SOURCE_ID> C00002 > <NAME> ATP;... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

3. Shell Programming and Scripting

Adding lines at a particular location in a file.

Hi Experts, Let us take a text file,say items.txt having the following data jar bottle gum tube cereal bag I want to add the content of items.txt to another file say #many lines not necessary ingredients #many line not necesary ingredients I want to append the data in... (3 Replies)
Discussion started by: Pradeep_1990
3 Replies

4. UNIX for Dummies Questions & Answers

Adding missing lines in file

Dear all, I have a file with two columns - the first column is increasing every 50, the second column is just count (e.g. 5). However, when count is zero, no line is present. Sample: How can I change the file so as to include lines with zero count? e.g. in the previous file to put... (4 Replies)
Discussion started by: TheTransporter
4 Replies

5. UNIX for Dummies Questions & Answers

Adding missing lines in file

Dear all, I have a file with two columns - the first column is increasing every 50, the second column is just count (e.g. 5). However, when count is zero, no line is present. Sample: 1950 7 2000 14 2050 7 2100 13 2150 10 2200 9 2250 7 2300 8 2350 7... (1 Reply)
Discussion started by: TheTransporter
1 Replies

6. UNIX Desktop Questions & Answers

Adding lines to pattern space

Is there any way to add lines to the pattern space of sed? I know a bit about the N flag, but am not able to use it to do what I want which is: read a file n lines at a time. If I find a match I quit else move to the next line. e.g. if I am looking for the following three lines Hello How... (2 Replies)
Discussion started by: jawsnnn
2 Replies

7. Shell Programming and Scripting

AWK adding prefix/suffix to list of strings

75 103 131 133 138 183 197 221 232 234 248 256 286 342 368 389 463 499 524 538 (5 Replies)
Discussion started by: chrisjorg
5 Replies

8. Shell Programming and Scripting

Adding strings to lines in a file

Hi all, I have a positional text file that comes from some source application. Before it is processed by destination application I have to add some header (suffix) to every record(line) in the file. e.g. Actual File ............... AccountDetails AcNO Name Amount 1234 John 26578 5678... (3 Replies)
Discussion started by: sharath160
3 Replies

9. UNIX for Dummies Questions & Answers

Adding lines and columns to a file

Hi everybody, I've got two simples file1 like: aaa aaa aaa bbb bbb bbb ccc ccc ccc and file2 like: 111 111 111 222 222 222 333 333 333 I need to: 1) add a line say "new line" as the first line of the file 2)add a column from file2 (say column3) to file1; the new column should... (14 Replies)
Discussion started by: zajtat
14 Replies

10. Shell Programming and Scripting

adding text to a file between lines

Suppose content of my first file: first line second line third line How can i insert text between "first line" & "second Iline" Any help?????/ (7 Replies)
Discussion started by: bishweshwar
7 Replies

Featured Tech Videos