Change in last line of each part of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change in last line of each part of a file
# 1  
Old 05-25-2012
Change in last line of each part of a file

Hi,

I have a input files as below.

Sample of one input file

Code:
type mtg_key =
 record
  decimal("\344") id;
  string("\344") ct_cd;
  string("\344") st_cd;
end;
type psl_key =
 record
  decimal("\344") id;
  utf8 string("\344") st_cd;
end;
type amk_fields =
record
  decimal("\344") tx_id;
  utf8 string("\344") ty_cd;
  decimal("\344") dvr_id;
end;


required output file
Code:
type mtg_key =
 record
  decimal("\344") id;
  string("\344") ct_cd;
  string("\344\006") st_cd;
end;
 
type psl_key =
 record
  decimal("\344") id;
  utf8 string("\344\006") st_cd;
end;
 
type amk_fields =
record
  decimal("\344") tx_id;
  utf8 string("\344") ty_cd;
  decimal("\344\006") dvr_id;
end;

condition

within each type and end;
i have to replace "\344" to "\344\006" in last line for each type in file.

Please, give your ideas, i will be thankfull to you

Thanks,
Aditya

Last edited by Franklin52; 05-25-2012 at 09:32 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 05-25-2012
Hmmmmm....

One way of doing it would be:

1) Reverse the input line-by-line. That is, make last line as first, 2nd from last as 2nd and so on.

2) In the "reversed" file, for each section starting with "end" and ending with "type", replace the 1st pattern (\344) with the required string (\344\006). Replacing the "first" pattern is much easier than replacing the "last" pattern because you don't know what "last" is.

3) Reverse the file again.


Just an idea...feel awk could do it...would you try it?
# 3  
Old 05-25-2012
Elixir_sinari's suggestion is definitely the most fool proof, but is it accurate to say if the last field equals "st_cd;" or "dvr_id;", then change 344 to 344\\006:

Code:
#!/bin/ksh
 
awk ' {
   if(($NF == "st_cd;") || ($NF == "dvr_id;") )
      gsub("344", "344\\006")
   print $0
} ' input.txt

# 4  
Old 05-26-2012
OK guys...

2) is a piece of cake for awk....

for 1), you can use
Code:
tac

for 3) use another
Code:
tac

on the output of awk.

How's that???
# 5  
Old 05-26-2012
How about perl ?
Code:
perl -pe '$/=undef;s/\(\"\\344\"\)(.*?)(\n\s*end;)/\(\"\\344\\006\"\)$1$2/mg;' filename

# 6  
Old 05-26-2012
Try:
Code:
awk '/type/{print x}1' infile | awk '{sub(/\\344/,"\\344\\006",$(NF-1))}1' FS='\n' OFS='\n' RS= ORS='\n\n'

# 7  
Old 05-28-2012
Thanks to all for your ideas... i am trying and working on it.

Thanks,
Aditya
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract a part of variable/line content in a file

I have a variable and assigned the following values ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 I need to read this variable from backward and stop read when I get first underscore (_) In this scenario I should get 211 Thanks Kris (3 Replies)
Discussion started by: mkris
3 Replies

2. UNIX for Dummies Questions & Answers

Grep a part of a line from a file

Hi, I have a file with thousands of lines as below INSERT INTO T_DIM_CLNT(CLNT_KY,CLNT_OBJ_ID,ISI_CLNT_ID,OPERN_ID,CLNT_NM,PRMRY_SIC_CD,PRMRY_SIC_DSC,RET_AGE_NBR,REC_CRT_TS,REC_DATA_EXTRC_TS,ETL_LOG_KY) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)... (5 Replies)
Discussion started by: sudhakar T
5 Replies

3. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

4. Shell Programming and Scripting

Shell script - Replace just part of a single line in a file.....

Hey guy's.... I new here, But im working on a school project, and I am not really good at programming. In fact, this is the only programming class that I need because programming is not what I am majoring in. But I have everything done in this shell script except for this last part..... ... (9 Replies)
Discussion started by: hxdrummerxc
9 Replies

5. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

6. UNIX for Dummies Questions & Answers

Making replicates of a file with part of a line randomized for each file

Ok, so let's say that I have a file like the following: I want to create 100 replicates of this file, except that for each file, I want different randomized combinations of either A or B at the end of each line so that I would end up with files like the following: and etc. I... (1 Reply)
Discussion started by: Scatterbrain26
1 Replies

7. Shell Programming and Scripting

Finding a string in a text file and posting part of the line

What would be the most succinct way of doing this (preferably in 1 line, maybe 2): searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found. In this case, it's known that there... (13 Replies)
Discussion started by: busdude
13 Replies

8. Shell Programming and Scripting

change part of a line with sed

Hi gurus, I'd like to change this complete line on a file: BAN_COMMAND="/etc/apf/apf -d $ATTACK_HOST {bfd.$MOD}" to this one: BAN_COMMAND="/sbin/iptables -A INPUT -s $ATTACK_HOST -j DROP" I've tried a lot without any successful . :( thanks in advance Israel. (2 Replies)
Discussion started by: iga3725
2 Replies

9. 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

10. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies
Login or Register to Ask a Question