LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2013, 07:38 PM   #1
sunilsagar
Member
 
Registered: Jul 2011
Posts: 30

Rep: Reputation: Disabled
rearranging the content in file


Hi Guys,

Need help with modifying the file using shell script.I have file as :

------------
Hostname1
------------
process1
process2
process3
------------
Hostname2
------------
process4
prcoess5
prcoess6
prcoess7
------------ and so on.

I need some syntax which I can use to update the file as :
Hostname1 process1
Hostname1 process2
Hostname1 process3
Hostname2 process4
Hostname2 process5
Hostname2 process6
Hostname2 process7

Now able to think , how can I start for this, please help. Thanks
 
Old 08-31-2013, 08:52 AM   #2
RockDoctor
Senior Member
 
Registered: Nov 2003
Location: Minnesota, US
Distribution: Fedora, Ubuntu, Manjaro
Posts: 1,791

Rep: Reputation: 427Reputation: 427Reputation: 427Reputation: 427Reputation: 427
You can start by writing down the steps you would use to perform the process manually
 
Old 08-31-2013, 01:18 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Here is how it may be done in sed:
Code:
$ sed -rn '/^-+$/{n;h;n;b;}; G; s/(.*)\n(.*)/\2 \1/p' infile
Hostname1 process1 
Hostname1 process2
Hostname1 process3
Hostname2 process4
Hostname2 prcoess5
Hostname2 prcoess6
Hostname2 prcoess7
First part of the "program" '/^-+$/{n;h;n;b;};' reads as follows: "if a line of dashes (/^-+$/) encountered, then read next line (n), save it in hold space (h), read next line and jump to end of the program (that is skip all other commands and start processing next line)". In other words we extract and save corresponding Hostname* here. For other lines we append hold space to pattern space (G), which gives us almost what we want, e.g. 'process1\nHostname1'. Now we need to swap strings on both sides of newline (\n) and remove newline (and print the result). The substitute (s) command 's/(.*)\n(.*)/\2 \1/p' does exactly that -- \1 corresponds to a string before newline and \2 -- to a string after newline.

Last edited by firstfire; 08-31-2013 at 01:25 PM.
 
1 members found this post helpful.
Old 09-01-2013, 11:25 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
------------
Carname
------------
Honda
Toyota
Nissan
Subaru
Mazda
------------
Fruitname
------------
apple
peach
orange
banana
------------
Veggiename
------------
potato
eggplant
onion
... this awk ...
Code:
awk '{if ($1=="------------") {getline;host=$1;getline;getline};
      print host,$0}'  $InFile >$OutFile
... produced this OutFile ...
Code:
Carname Honda
Carname Toyota
Carname Nissan
Carname Subaru
Carname Mazda
Fruitname apple
Fruitname peach
Fruitname orange
Fruitname banana
Veggiename potato
Veggiename eggplant
Veggiename onion
Daniel B. Martin

Last edited by danielbmartin; 09-01-2013 at 09:06 PM. Reason: Tighten the code
 
  


Reply

Tags
awk, bash scripting, sed, shell script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help with rearranging file system yaximik Linux - Newbie 3 06-07-2013 11:53 PM
Write a script which copies content of file 1 to file 2 without using cp command. aashka Linux - Newbie 5 04-10-2012 03:55 PM
Dividing content of one file by content of another larspend Linux - Newbie 5 04-12-2011 08:00 PM
[SOLVED] Sed – how do I save output to file with filename from content of another file? misarab Programming 4 02-28-2011 02:12 PM
how to create a new file including content from reading another file !? silverhand Programming 2 10-06-2007 04:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration