Edit other than the first and last record of a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Edit other than the first and last record of a text file
# 1  
Old 06-24-2009
Edit other than the first and last record of a text file

Hello,

I have to add the string "YES" to the end of all records of files in a dir other than the first(header) and the last(trailer) record. viz, the first and the last record in a file has to be identified and exempted from the edit.

I tried an awk script. But there seems to be no direct way of identifying the last record.

viz,
Original File contains :

<Header>
<Record1>
<Record2>
<Trailer>

The modified file should contain :

<Header>
<Record1>YES
<Record2>YES
<Trailer>

There is no fixed pattern for the values in <Header> and <Trailer>
# 2  
Old 06-24-2009
Code:
awk '{if(NR!=1 && NR !=_last_line){print}}' _last_line="`wc -l inputfile|awk '{print $1}'`" inputfile

# 3  
Old 06-24-2009
Quote:
Originally Posted by rakeshawasthi
Code:
awk '{if(NR!=1 && NR !=_last_line){print}}' _last_line="`wc -l inputfile|awk '{print $1}'`" inputfile

rakeshawasthi, have you test the your command? It doesn't give the desired output!

rogersed, you can try this:

Code:
awk '
NR==FNR{if($0 ~ /Header/){n=NR}next}
FNR>n && !/Trailer/{print $0 "YES";next}
1' file file

Regards
# 4  
Old 06-24-2009
Quote:
Originally Posted by Franklin52
rakeshawasthi, have you test the your command? It doesn't give the desired output!

rogersed, you can try this:

Code:
awk '
NR==FNR{if($0 ~ /Header/){n=NR}next}
FNR>n && !/Trailer/{print $0 "YES";next}
1' file file

Regards
Sorry not read the requirement properly.

Code:
awk '{if(NR != 1 && NR !=_last_line){print $0" YES"}else {print}}' _last_line="`wc -l inputfile | awk '{print $1}'`" inputfile

# 5  
Old 06-24-2009
Code:
function append_content
{
file=$1
sed -n '1{
        p
        }
        1 !{
        ${
        p
        }
        $!{
        s/$/YES/
        p
        }
}' $file > ${file}.bak
mv $file.bak $file
}
for i in *.txt;do
append_content $i
done

# 6  
Old 06-24-2009
Quote:
Originally Posted by rakeshawasthi
Sorry not read the requirement properly.

Code:
awk '{if(NR != 1 && NR !=_last_line){print $0" YES"}else {print}}' _last_line="`wc -l inputfile | awk '{print $1}'`" inputfile

rakeshawasthi,

Please test your command before you post it, it doesn't work as desired.

Regards
# 7  
Old 06-24-2009
Try...
Code:
sed '1n;$q;s/$/YES/' file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to edit a text file

hi, could someone share a short script that would process a .txt file and do the following, for example the text file has this form 0:1.0 1:1.0 2:2.0 3:3.0 4:4.0 5:5.0 6:6.0 7:7.0 8:8.0 ... {newline} 9:9.0 10:10.0 11:11.0 12:12.0 13:13.0 14:14.0 15:15.0 16:16.0 17:17.0 ... {newline} and I... (3 Replies)
Discussion started by: c_lady
3 Replies

2. Shell Programming and Scripting

Edit first line of a text file

Hi friends, Issue1: I have a text file with the first line like this #chrom start end Readcount_A Normalized_Readcount_A ReadcountB Normalized_Readcount_B Fc_A_vs_B pvalue_A_vs_B FDR_A_vs_B Fc_B_vs_A pvalue_B_vs_A FDR_B_vs_A <a href="http://unix.com/">Link</a> How can I change it to the... (11 Replies)
Discussion started by: jacobs.smith
11 Replies

3. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

4. Shell Programming and Scripting

delete text from each record in a file

Hi guys, I have been given a small task to do and I am stuck already. I have to format a file with people's emails address in it ready for pasting into the BCC section of an email. The file looks like this:- bob@ibm.com SMTP BOB SMITH text text text sue@icl.org SMTP Susy Smith text text... (8 Replies)
Discussion started by: joe_evans
8 Replies

5. Shell Programming and Scripting

Open the file and edit/append the text

Hi i have a file like this mailboxnum 20 filename <to subsitute> fileloaction /home/dd234/ filetype txt in a directory i have set of files for ex: TT45.1.2 TT45.1.3 TT45.1.4 . . in for loop i have to take this files and subsitute one by one ex: (1 Reply)
Discussion started by: greenworld123
1 Replies

6. Shell Programming and Scripting

Loop through text file > Copy Folder > Edit XML files in bulk?

I have a text file which contains lines in this format - it contains 105 lines in total, but I'm just putting 4 here to keep it short: 58571,east_ppl_ppla_por 58788,east_pcy_hd_por 58704,east_pcy_ga_por 58697,east_pcy_pcybs_por It's called id_key.txt I have a sample folder called... (9 Replies)
Discussion started by: biscuitcreek
9 Replies

7. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

8. UNIX for Dummies Questions & Answers

edit this text file

hi, i need to remove the first column (and dash) from this text file. Is there any unix command allowing me to do it ? 1-16 2-28 3-16 4-20 5- 8 6-32 7-19 8-16 9-27 10- 7 11-14 12-18 thanks (2 Replies)
Discussion started by: aneuryzma
2 Replies

9. Shell Programming and Scripting

Read the record from a text file

Hi I want to read one row record from a text file. For eg: I have Sample.txt file with one row of record like 123456768 I want to get the above value from the file and assign it to a variable in my script. Please guide me how to proceed. Thanks, Soll (2 Replies)
Discussion started by: sollins
2 Replies

10. Shell Programming and Scripting

Edit and insert character in a text file

Hello All, Can somebody please help me how to accomplish the following : I have a text file called data.txt that has the following information : M|88494-998494 M|98jd#0094 M|88394-994049 M|GFG9980#009944 and so on... I need to replace the value of M to either "S" or "X"... (3 Replies)
Discussion started by: negixx
3 Replies
Login or Register to Ask a Question