How to modify the last line in the file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to modify the last line in the file.
# 1  
Old 12-12-2007
How to modify the last line in the file.

Hi,

I have data file which has header, data and trailer.

The problem is that always the trailer is padded with the last line data.
i want to split the trailer and bring it to next line i.e end of line.

But i dont have any specific string to identify the trailer. Only way is that all data are 1234 characters. so i want to bring the last line characters from 1235 to next line.

Can anyone help on this
# 2  
Old 12-12-2007
I don't fully understand what you are wanting to do here, but tail -c x should be able to get you the last x many characters.
# 3  
Old 12-12-2007
If the last line of the file has more than 1234 characters, i want to cut the character position from 1235 to end of the line and put it as next line.

Example File:

HDR1234343545
abcdefghijklmnopqrst
TRLdefghijklmnopqrst
TRLdefghijklmnopqrst
abcdefghijklmnopqrst
abcdefghijklmnopqrst
abcdefghijklmnopqrstTRL0007

I want to cut the last line characters from 21 and make it as new line.

The output should be like this


HDR1234343545
abcdefghijklmnopqrst
TRLdefghijklmnopqrst
TRLdefghijklmnopqrst
abcdefghijklmnopqrst
abcdefghijklmnopqrst
abcdefghijklmnopqrst
TRL0007
# 4  
Old 12-12-2007
Ah, now I understand... You want to insert a newline 1234 from the end of the file?

_way_ ugly but it should do the job.
Code:
#!/bin/sh
offset=$1
filename=$2
filesize=`wc -c $filename | awk '{ print $1 }'`
remainder=`expr $filesize - $offset`
echo $filesize $remainder $offset
dd bs=1 if=$filename count=$remainder 2> /dev/null
echo ""
tail -${offset}c $filename

scriptname.sh 1234 filename.txt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Modify text file if found multiple pattern match for every line.

Looking for help, i have input file like below and want to modify to expected output, if can without create additional file, hope can direct modify it. have 2 thing need do. 1st is adding a word (testplan generation off) after ! ! IPG: Tue Aug 07 14:31:17 2018 2nd is adding... (16 Replies)
Discussion started by: kttan
16 Replies

2. Shell Programming and Scripting

Modify one line in a plain text file

Hi everyone, I want to know, if there is a way to modify one line in a text file with unix script, with out re-writing all the file. For example, i have this file: CONFIGURATION_1=XXXX CONFIGURATION_2=YYYY CONFIGURATION_3=ZZZZ supose i have a command or function "modify" that... (7 Replies)
Discussion started by: Xedrox
7 Replies

3. Shell Programming and Scripting

Modify a file by another file: add new line and variable after string is found

hello, I have problem with writing/adjusting a shell script. I searched forum and unfortunately couldn't write scipt based on the information I found. I never wtire such so it's hard for me and I do need to modify one script immediately. case looks like: 1. 'file' that needs to be modified... (3 Replies)
Discussion started by: bipbip
3 Replies

4. Shell Programming and Scripting

Modify line with dynamic variable in awk

Hi, I'm guessing this is probably relatively straight forward to do in awk, but I just can't get my head round it! I have a log file of the following format: 3:03:35 (lmgrd) TIMESTAMP 10/14/2011 3:20:41 (MLM) IN: "MATLAB" user1@host1.private.dns.zone 3:21:05 (MLM) IN: "MATLAB"... (2 Replies)
Discussion started by: chrissycc
2 Replies

5. Shell Programming and Scripting

How to use sed to modify a line above or below matching pattern?

I couldn't figure out how to use sed or any other shell to do the following. Can anyone help? Thanks. If seeing a string (e.g., TODAY) in the line, replace a string in the line above (e.g, replace "Raining" with "Sunny") and replace a string in the line below (e.g., replace "Reading" with... (7 Replies)
Discussion started by: sprinner
7 Replies

6. Shell Programming and Scripting

Help with a shell script to modify one line and copy the next 9 to same file

Hi everyone, the problem is quite simple, yet I can't find an easy solution using awk. I need to search for a string in $3, then if I find this string, copy the line,modify $3, and copy the next 9 lines to the same file. My problem is in the copying of the lines... Finding and modifying... (5 Replies)
Discussion started by: Teroc
5 Replies

7. Shell Programming and Scripting

Modify sections of the line in a file

Hello.. I have a line in a file which I have to edit: the line looks like: <!]> Sometimes, the section of the line can have only one entry for cn, or maybe more than 2 like below: <!]> I have a variable which has the following value: CN="(cn=MNO)(cn=XYZ)" I need to replace the part... (4 Replies)
Discussion started by: chiru_h
4 Replies

8. Shell Programming and Scripting

Need help to modify perl script: Text file with line and more than 1 space

Dear Friends, I am beginner in Perl and trying to find the problem in a script. Kindly help me to modify the script. My script is not giving the output for the last field and followed text (LA: Language English). Input file & script as follows: Input file: Thu Mar 19 2:34:14 EDT 2009 STC... (3 Replies)
Discussion started by: srsahu75
3 Replies

9. Shell Programming and Scripting

Modify Specific Line of a Text File

Given a text file, how do you add a line of text after a specific line number? I believe I would want to use "sed" but I am unsure of the syntax. Thank you. Mike (5 Replies)
Discussion started by: msb65
5 Replies

10. UNIX for Dummies Questions & Answers

How to modify a line in a file

Hello I have an input that looks like this: d:/bla1/bla2/bla3/bla1.txt d:/bla1/bla2/bla4/bla2.txt d:/bla1/bla2/bla5/bla1.txt d:/bla1/bla2/bla6/bla2.txt d:/bla1/bla2/bla7/bla1.txt and i need the output to looks like this: d:/bla1/bla2/bla3/bla1_bla3.txt... (5 Replies)
Discussion started by: RoadKill
5 Replies
Login or Register to Ask a Question