Renaming all header to specific header pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming all header to specific header pattern
# 1  
Old 12-15-2009
Renaming all header to specific header pattern

Input
Code:
#HAC0253
EFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV

#BASFS12
EFVEFVHIJEFVEFVTOPKEFVOPKTHIJTTHIJOPK

#ACG5115
TEFVEFVOIJEFVHIJHIJOPKOPKHIJHIJTTEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK

#ECG5114
IJTOPKHIJEFVOEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK
.
.

Output
Code:
#MG_PN_0001SPV001
EFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV

#MG_PN_0002SPV001
EFVEFVHIJEFVEFVTOPKEFVOPKTHIJTTHIJOPK

#MG_PN_0003SPV001
TEFVEFVOIJEFVHIJHIJOPKOPKHIJHIJTTEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK

#MG_PN_0004SPV001
IJTOPKHIJEFVOEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK
.
.

Depending on how many header available, I hope able to rename all the header to this specific format like : MG_PN_0001SPV001,MG_PN_0002SPV001,MG_PN_0003SPV001,...,etc
# 2  
Old 12-15-2009
Code:
cat t.pl
$i = 1;

while(<>)  {
        if ( /^#/ )  {
                printf "#%s", "MG_PN_";
                printf "%04d", $i++;
                print "SPV001\n";
        }  else  {
                print;
        }
}


Code:
18:47:31 : ~ : cat t
#HAC0253
EFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV

#BASFS12
EFVEFVHIJEFVEFVTOPKEFVOPKTHIJTTHIJOPK

#ACG5115
TEFVEFVOIJEFVHIJHIJOPKOPKHIJHIJTTEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK

#ECG5114
IJTOPKHIJEFVOEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK
18:47:32 : ~ : perl t.pl t
#MG_PN_0001SPV001
EFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV

#MG_PN_0002SPV001
EFVEFVHIJEFVEFVTOPKEFVOPKTHIJTTHIJOPK

#MG_PN_0003SPV001
TEFVEFVOIJEFVHIJHIJOPKOPKHIJHIJTTEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK

#MG_PN_0004SPV001
IJTOPKHIJEFVOEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK

# 3  
Old 12-15-2009
Code:
awk '{if ($1~/^#/) {printf "MG_PN_%04d_SPV001\n", ++i} else {print}}' input.txt

# 4  
Old 12-16-2009
Code:
gawk ' /^#/{
   pad=sprintf("%04d",++c)
   str="MG_PN_"pad"SPV001"
   $0="#"str
} {print}' file

# 5  
Old 12-18-2009
Thanks, thegeek.
Your perl script work perfectlySmilie
Thanks again.

---------- Post updated at 06:10 AM ---------- Previous update was at 06:09 AM ----------

Thanks for your suggestion, rdcwayx.
It works fine for my problem ^^

---------- Post updated at 06:20 AM ---------- Previous update was at 06:10 AM ----------

Thanks ichigo,
Your code is another way to solve my problem too ^^
# 6  
Old 12-19-2009
if you have Python
Code:
#!/usr/bin/env python
counter=1
for line in open("file"):
    sl=line.strip()
    if sl.startswith("#"):
        counter=str(counter).zfill(4)
        new="MG_PN_"+counter+"SPV001"
        counter=int(counter)+1
        sl="#"+new
    print sl

output
Code:
$ ./python.py
#MG_PN_0001SPV001
EFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV

#MG_PN_0002SPV001
EFVEFVHIJEFVEFVTOPKEFVOPKTHIJTTHIJOPK

#MG_PN_0003SPV001
TEFVEFVOIJEFVHIJHIJOPKOPKHIJHIJTTEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK

#MG_PN_0004SPV001
IJTOPKHIJEFVOEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. UNIX for Beginners Questions & Answers

Keep only columns in first two rows based on partial header pattern.

I have this code below that only prints out certain columns from the first two rows (doesn't affect rows 3 and beyond). How can I do the same on a partial header pattern “G_TP” instead of having to know specific column numbers (e.g. 374-479)? I've tried many other commands within this pipe with no... (4 Replies)
Discussion started by: aachave1
4 Replies

3. Shell Programming and Scripting

Manipulate all rows except header, but header should be output as well

Hello There... I have a sample input file .. number:department:amount 125:Market:125.23 126:Hardware store:434.95 127:Video store:7.45 128:Book store:14.32 129:Gasolline:16.10 I will be doing some manipulations on all the records except the header, but the header should always be... (2 Replies)
Discussion started by: juzz4fun
2 Replies

4. Shell Programming and Scripting

Add column header and row header

Hi, I have an input like this 1 2 3 4 2 3 4 5 4 5 6 7 I would like to count the no. of columns and print a header with a prefix "Col". I would also like to count the no. of rows and print as first column with each line number with a prefix "Row" So, my output would be ... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

Specific Header after every 30 records

Hi All, I have got a requirement. I have a source file, EMPFULL.txt and I need to split the data for every 30 records and place a Typical Header as below with system and page number too. 2012.01.03 Employee Dept Report 1... (6 Replies)
Discussion started by: srk409
6 Replies

6. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

7. Shell Programming and Scripting

Need awk help to print specific columns with as string in a header

awk experts, I have a big file of 4000 columns with header. Would like to print the columns with string value of "Commands" in header. File has "," separator. This file is on ESX host with Bash. Thanks, Arv (21 Replies)
Discussion started by: arv_cds
21 Replies

8. Shell Programming and Scripting

Remove specific pattern header and its content problem facing

Input file: >TRACK: Position: 1 TYPE: 1 Pos: SVAVPQRHHPGGTVFREPIIIPAIPRLVPGWNKPIIIGRHAFGDQYRATDRVIPGPGKLE LVYTPVNGEPETVKVYDFQGGGIAQTQYNTDESIRGFAHASFQMALLKGLPLYMSTKNTI LKRYDGRFKDIFQEIYESTYQKDFEAKNLWYEHRLIDDMVAQMIKSEGGFVMALKNYDGD >TRACK: Position: 1 TYPE: 2 Pos: FAHASFQMALLKGLPLYMS... (8 Replies)
Discussion started by: patrick87
8 Replies

9. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies
Login or Register to Ask a Question