Help with script to add two zeros to certain lines in a set of files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with script to add two zeros to certain lines in a set of files?
# 1  
Old 07-29-2011
Help with script to add two zeros to certain lines in a set of files?

Hello... I should be better with scripting but I am not so turning here for some help. I did search quite a bit using google and didn't find anything meeting my needs for this. What am after here is a script (in a redhat linux env) that will read in a small series of files (netbackup vault reports) and add two zeros to the beginning of any line segment that has an L2 or L4 in it.. this is what I came up with to augment the part of the files

Code:
cat *.rpt | egrep "(L2)|(L4)" | awk '{t="00"}{ print t""$1 }'

this works but if only digs out the part of the file to be edited. I need to edit the files adding the zeros while preserving the original file names and other file context. I searched around here and found another script and tried to merge my line with that script but it manged the test files.. ugh. Here is what I had..

Code:
for i in *.rpt;do cat $i | egrep "(L2)|(L4)" | awk '{t="00"}{ print t""$1 }' $i > ${i}_ && mv -f ${i}_ $i;done

it added the zeros but did it to each line and ripped the files apart (deleted everything after the first line of the file).. Anyways.. Can someone offer some me some help on this? Thanks much!
# 2  
Old 07-29-2011
Yes,
please post a small sample of your input and an example of the expected/desired output.
# 3  
Old 07-29-2011
Oh thanks so much for responding.. and so quickly!

here is the input file

Quote:
05/02/2011 Page: 1

Robot: TLD(0) Vault: DPSI Profile: Vault
Vendor: DPSI Session ID: 1818

LTO2 - return list - media to return onsite

MEDIA SLOTID LAST MOUNT LAST SID REQUESTED DENSITY CATEGORY


4063L2 1927 04/27/2010 1818 05/02/2011 HCART2 NBU
4064L2 1928 04/27/2010 1818 05/02/2011 HCART2 NBU
4067L2 1929 04/27/2010 1818 05/02/2011 HCART2 NBU
9703L2 1769 01/18/2010 1818 05/02/2011 HCART2 NBU
9862L2 1783 01/20/2010 1818 05/02/2011 HCART2 NBU


TOTALS

Number of Media: 5
here is how I would like the outcome to look

Quote:
05/02/2011 Page: 1

Robot: TLD(0) Vault: DPSI Profile: Vault
Vendor: DPSI Session ID: 1818

LTO2 - return list - media to return onsite

MEDIA SLOTID LAST MOUNT LAST SID REQUESTED DENSITY CATEGORY


004063L2 1927 04/27/2010 1818 05/02/2011 HCART2 NBU
004064L2 1928 04/27/2010 1818 05/02/2011 HCART2 NBU
004067L2 1929 04/27/2010 1818 05/02/2011 HCART2 NBU
009703L2 1769 01/18/2010 1818 05/02/2011 HCART2 NBU
009862L2 1783 01/20/2010 1818 05/02/2011 HCART2 NBU


TOTALS

Number of Media: 5
So I want to add two zeros to the beginning of the media ID for the tapes. Each file segment (media ID) I want to change ends in either an L2 or an L4... so those are the identifiers I was using.. but perhaps there is a better way to do this?
Thanks so much for your time.
# 4  
Old 07-29-2011
Code:
perl -i.orig -pe'
  s/(\d*L[24])/00$1/
  ' file1 [.. file2]

This User Gave Thanks to radoulov For This Post:
# 5  
Old 07-29-2011
wow... I had a feeling it would be a small bit of code. Thanks so much.
If I want to have it run on 4 files in the same directory all ending in .rpt do I use a 'for' 'next' or 'while' 'do' type loop? Could you help me with the perl syntax for that?

again, many thanks for your help!!

edit: basically I have 4 new files each week generated that I will cron this script to edit. So each week I will run it against 4 new files, then ftp them (got the script for that done) and then move the files to a "sent" dir once it's all done. If I can get the auto editing bit done I think I can sort the rest on my own. Thanks!
# 6  
Old 07-29-2011
Just pass the filenames to the Perl script:

Code:
perl -i.orig -pe'
  s/(\d*L[24])/00$1/
  ' *.rpt

P.S. The script backs up the original versions first, if you don't need that just remove the .orig part:

Code:
perl -i -pe ...

This User Gave Thanks to radoulov For This Post:
# 7  
Old 07-29-2011
Oh.. *doink* I see. thanks again for your speedy help on this.

edit: worked like a charm!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

2. Shell Programming and Scripting

How to add a line to the end of a set of files without using sed command?

I understand that the SED command reads all the lines in the file before adding a required line to the end of the file. Is there another command that adds a line to the end of files without reading the entire file.... SED is increasing the processing time as the number of lines in each of the... (1 Reply)
Discussion started by: Kanch
1 Replies

3. UNIX for Dummies Questions & Answers

LINUX - How to add a prefix to a set of files

Hi All, I have a file (lets say filenames.txt) which contains a list of file names. Is there a way I can append a prefix to the filenames as below, Thanks much for your help Freddie (3 Replies)
Discussion started by: dsfreddie
3 Replies

4. UNIX for Advanced & Expert Users

SQL script with 86000 lines: new files with only 10000 lines (per file)

Hi this is my SQL script $ wc -l insert_into_customers.sql 85601 insert_into_customers.sqlI wish to cut this file into 9 files each 10000 lines (the last one less) $ wc -l insert_into_customers_00*.sql 10000 insert_into_customers_001.sql 10000 insert_into_customers_002.sql ... (1 Reply)
Discussion started by: slashdotweenie
1 Replies

5. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

6. UNIX for Dummies Questions & Answers

Add leading zeros to columns in a file

Hello Gurus, Quick question. I have a file with the following records: A~000000000000518000~SLP ~99991231~20090701~88.50~USD~CS~ A~000000000000518000~SLP ~99991231~20090701~102.00~USD~CS~ A~000000000000772000~SLP ~99991231~20100701~118.08~USD~CS~ I wold like to do the following: 1. Add... (1 Reply)
Discussion started by: chumsky
1 Replies

7. Shell Programming and Scripting

Add leading zeros in floating point variable

I need to add leading zeros in a floating point numbers. The length of the number should be 13 including decimal. The input number is changing so number of leading zeros is not fix. For example input output 216.000 000000216.000 1345.000 000001345.000 22345.500 ... (4 Replies)
Discussion started by: reeta_shri
4 Replies

8. Shell Programming and Scripting

Drop common lines at head/tail of a large set of files

Hi! I have a large set of pairs of text files (each pair in their own subdirectory) and each pair shares head/tail (a couple of first and last lines) but differs in the middle part. I need to delete the heads/tails and keep only the middle portions in which they differ. The lengths of heads/tails... (1 Reply)
Discussion started by: dobryden
1 Replies

9. Shell Programming and Scripting

Help!! Need script to read files and add values by lines...

Hi All, I really need your help. I am a begginner in shell script and I believe this is a very simple issue. I have in my directory, n-files, like 1.dhm, 2.dhm, 3.dhm. These files have 1 column with 1 value per line, like: 1.dhm ------ 10 20 30 40 50 2.dhm ------ 30 50 20 (3 Replies)
Discussion started by: dhuertas
3 Replies
Login or Register to Ask a Question