LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 10-15-2007, 12:29 PM   #1
OnoTadaki
LQ Newbie
 
Registered: Mar 2004
Posts: 11

Rep: Reputation: 0
Help with script to batch edit text files


Hello! I am looking for some help editing a massive list from format A to format B.

Example below:


Format A:

1
----------
entry #1
entry #2

123
----------
entry #1
entry #2
entry #3

1500
----------
entry #1
entry #2

2000
----------
entry #1
entry #2
entry #3
entry #4


To Format B:


entry #1 (1)
entry #2 (1)

entry #1 (123)
entry #2 (123)
entry #3 (123)

entry #1 (1500)
entry #2 (1500)

entry #1 (2000)
entry #2 (2000)
entry #3 (2000)
entry #4 (2000)


As you can see, the database does not have any leading zeros on the heading numbers and each heading can have a different number of entries ranging from one to hundreds.


I would appreciate any support you all could give me trying to figure out some faster way to edit a couple thousand entries than by hand! I have no idea where to even start with this exhaustive endeavour.

Thanks!
 
Old 10-15-2007, 12:38 PM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Code:
awk '/^[0-9]/{v=" ("$0")";next};/^--*/{next};NF{$0=$0v}1' A>B

Last edited by radoulov; 10-15-2007 at 12:46 PM.
 
Old 10-15-2007, 12:44 PM   #3
OnoTadaki
LQ Newbie
 
Registered: Mar 2004
Posts: 11

Original Poster
Rep: Reputation: 0
Thank you very much! This is a great help!
 
Old 10-15-2007, 12:59 PM   #4
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
For files as A_1 A_2 ... A_n:
Code:
awk '/^[0-9]/{v=" ("$0")";next};/^--*/{next};{print NF?$0=$0v:"">FILENAME"_B"}' A_*
The modified versions will be A_1_B, A_2_B ... A_n_B.
 
Old 10-15-2007, 01:11 PM   #5
OnoTadaki
LQ Newbie
 
Registered: Mar 2004
Posts: 11

Original Poster
Rep: Reputation: 0
Thank you again I really have to learn advanced bash techniques.
 
Old 10-15-2007, 02:44 PM   #6
student04
Member
 
Registered: Jan 2004
Location: Georgia
Distribution: OS X, CentOS
Posts: 669

Rep: Reputation: 34
Quote:
Originally Posted by OnoTadaki View Post
Thank you again I really have to learn advanced bash techniques.
That's all awk.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Batch Script to rename files... jamie_barrow Linux - Newbie 16 06-14-2009 01:26 PM
Bash script to edit text file snowman81 Linux - Desktop 2 01-10-2007 03:33 PM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 06:52 AM


All times are GMT -5. The time now is 11:54 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration