Visit Our UNIX and Linux User Community


Change one line to multiple


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change one line to multiple
# 1  
Old 01-20-2014
Change one line to multiple

I did help some at another forum to change one line to multiple lines.
Code:
var1="abc001: text goes here yyy003: text goes here uuuu004: text goes here"

Running this awk, gives correct result, but its not very nice. Any idea on how to simplify it?
Code:
awk '{for (i=1;i<=NF;i++) printf $i~":"?"\n"$i" ":$i" "}' <<< "$var1"| awk 'NF {sub(/:/,x);print}'
abc001 text goes here
yyy003 text goes here
uuuu004 text goes here

Change to one line, remove :
# 2  
Old 01-20-2014
Code:
awk '{ gsub(/([^ ]*): /, "\n\1"); sub(/^\n/,""); } 1'

# 3  
Old 01-20-2014
Thanks Corona, but hostname is missing.

Another awk
Code:
awk '{gsub(/[a-zA-Z]+[0-9]+/,"\n&");gsub(/:|^\n/,x)}1' <<< "$var1"
abc001 text goes here
yyy003 text goes here
uuuu004 text goes here

^/n did help me to remove the blank line

Last edited by Jotne; 01-20-2014 at 05:48 PM..
# 4  
Old 01-20-2014
Perl solution:
Code:
perl -0pe 's/://;s/(\w+):/\n$1/g' <<< "$var1"

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 01-20-2014
Hello,

One more approach.

Code:
echo $var1  | awk 'gsub(/\:/,X) gsub(/here /,"here\n");'

Output will be as folllows.

Code:
abc001 text goes here
yyy003 text goes here
uuuu004 text goes here



Thanks,
R. Singh
# 6  
Old 01-20-2014
@R.Sing
Will not work, sine this is just an example text and it will change.
One static is the :
# 7  
Old 01-20-2014
You could also do bartus11's trick in sed:

Code:
sed -r 's/://;s/(\w+):/\n\1/g' <<< "$var1"


Last edited by Chubler_XL; 01-20-2014 at 06:24 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #935
Difficulty: Medium
A fixed 32-bit data format for system time means that timestamps rolls over approximately every 136 years, with the first such occurrence on 7 February 2036.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

3. UNIX for Dummies Questions & Answers

Want to change common line from multiple files

Hi everyone, I've a requirement to modify an existing line which is common to multiple files. I need to replace that existing line with a new line. I've almost 900 ksh files to edit in the similar fashion in the same directory. Example: Existing Line: . $HOME/.eff.env (notice the "." at the... (3 Replies)
Discussion started by: kaleem.adil
3 Replies

4. Shell Programming and Scripting

Shell script to read multiple options from file, line by line

Hi all I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis. The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
Discussion started by: haggismn
1 Replies

5. UNIX for Dummies Questions & Answers

Formatting Multiple fields on 1 line to multiple rows

I'm trying extract a number of filename fields from a log file and copy them out as separate rows in a text file so i can load them into a table. I'm able to get the filenames but the all appear on one line. I tried using the cut command with the -d (delimiter) option but cant seem to make it... (1 Reply)
Discussion started by: Sinbad-66
1 Replies

6. Shell Programming and Scripting

Need to find a multiple line block and replace with a multiple line block

I would perfer to use cut and paste to do this but I can't find a GUI to do this with. What I want to do is to find a multiple line block of code like Exit Sub Log_Handler: then replace it with GoTo RSLogRtn Exit Sub Log_Handler: Basically it is just an insert, but I may want to... (8 Replies)
Discussion started by: Randem
8 Replies

7. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

8. Shell Programming and Scripting

Script to change file contents line by line

Hi, I'm struggling to write a script to do the following, -will go through each line in the file -in a specific character positions, changes the value to a new value -These character positions are fixed througout the file ----------------------- e.g.: file1.sh will have the following 3... (4 Replies)
Discussion started by: vini99
4 Replies

9. Shell Programming and Scripting

Change a Char Multiple line records

Hi All i have a file that is to big for vi and is a multiple line record 3999||20090127163547796|196.46.162.250|1028|196.207.40.112|2152|00:0C:31:BB:25:5 4|00:00:0C:07:AC:06|655016000575511|05||3C65|0D029C1D|||00644B5A|||||||||||inter... (5 Replies)
Discussion started by: gseptember
5 Replies

10. Shell Programming and Scripting

rsh to change multiple ip in multiple servers?

good day. i jsut wanted to know what is the best script or the best way changing a lot of Ip's in all servers. Do you have any idea? im using awk to change IP,what if, you have lots of servers. You need to change it one by one? It will take time to change it manually. (2 Replies)
Discussion started by: kenshinhimura
2 Replies

Featured Tech Videos