Help with shell script: moving end of line character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with shell script: moving end of line character
# 1  
Old 11-10-2009
Question Help with shell script: moving end of line character

Hello.

I have a file (old.txt) that I need to copy into another file (new.txt).

Each line on old.txt ends with CR/LF but the position of CR/LF varies from one record to another.

I need to copy each line of record to new.txt and move CR/LF in pos 165.

Can I use awk to achieve this? How? If not, what other options can be used?

Thank you. Your response(s) are greatly appreciated.
# 2  
Old 11-10-2009
For copying the lines except the last character ,you can use something like this :

Code:
awk '{ print substr($0,1,length($0)-1)}' filename.txt

# 3  
Old 11-10-2009
Did u mean u need to append spaces for the rest of the line till 165th character...

Then u can adapt

awk '{printf "%-165s\n",$0}' old.txt > new.txt

All lines will be left justified for 165 characters...
# 4  
Old 11-10-2009
Quote:
Originally Posted by bittoo
Did u mean u need to append spaces for the rest of the line till 165th character...

Then u can adapt

awk '{printf "%-165s\n",$0}' old.txt > new.txt

All lines will be left justified for 165 characters...
If CR/LF is found at position 100, I would need to copy all characters from 1-99, and append spaces from position 100-164.

How do we do this?
# 5  
Old 11-10-2009

Ohh.. is it something like this ?.

Code:
awk '{ print substr($0,1,length($0)-1);for(i=length($0)+1;i<165;i++) print " "}' filename.txt

# 6  
Old 11-10-2009
Can u try the above comamnd on ur file and let me know the result.. If that does not work, let us give a simple input and ur expected output
# 7  
Old 11-10-2009
Quote:
Originally Posted by panyam

Ohh.. is it something like this ?.

Code:
awk '{ print substr($0,1,length($0)-1);for(i=length($0)+1;i<165;i++) print " "}' filename.txt

this is my test data:
Code:
HEADER    BBBBB_gllvdcout  20091106.1700040715
AA  *     EAA        EAAAAAA     6AAAAAAAA0     Y AAAA AAA SAAAAA MAAAAAAAAAA
AB  *     EBB        EBBBBBB     6BBBBBBBB0       BBBB BBB BBBBBB BBBBBBBBBBB
TRAILER   351845           20091106.170004

when I tried the above command I get this:

Code:
HEADER    BBBBB_gllvdcout  20091106.170004071
 .
 .
 .
 .
 .
 AA  *     EAA        EAAAAAA     6AAAAAAAA0     Y AAAA AAA SAAAAA MAAAAAAAAA
 .
 .
 .
 .
 .
 AB  *     EBB        EBBBBBB     6BBBBBBBB0       BBBB BBB BBBBBB BBBBBBBBBB
 .
 .
 .
 .



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

when i tried this:
Code:
awk '{printf "%-165s\n",$0}' old.txt > new.txt

I get this:
Code:
HEADER    BBBBB_gllvdcout  20091106.1700040715                                                                                                                       
AA  *     EAA        EAAAAAA     6AAAAAAAA0     Y AAAA AAA SAAAAA MAAAAAAAAAA                                                                                        
AB  *     EBB        EBBBBBB     6BBBBBBBB0       BBBB BBB BBBBBB BBBBBBBBBBB                                                                                        
TRAILER   351845           20091106.170004

CR/LF is at position 166.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to check line end not ending with comma

I have several line in a text file. for example I like apple; I like apple I like orange; Output: I like apple I try to useif grep -q "!\;$"; then (Not work) Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules). (1 Reply)
Discussion started by: cmdcmd
1 Replies

2. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

3. Shell Programming and Scripting

How to add a character at end of line?

Hai, I have got a small requirement in my script. and i am using bash shell. I need to add a dot (.) for some particular line in a file. Say for example, $Cat rmfile 1 This is line1 2 This is line2 3 This is line3 O/p should be : $Cat rmfile 1 This is line1 2 This is line2. #... (2 Replies)
Discussion started by: Sivajee
2 Replies

4. Shell Programming and Scripting

Adding end of line character in the last record

Need to add end of line character to last record in a fixed width file. When i take the record length of each line, for all the records it gives example like 200 except the last line as 199. Due to this my other script fails. Please help me on this. (4 Replies)
Discussion started by: Amrutha24
4 Replies

5. Shell Programming and Scripting

How does a shell script recognize the end of a line?

Hi friends , I want to know how does a shell script recognize the end of a line? . i have hunddres of proccedure to test where i want to ingnore the comments which starts with "--" .. it can start from the middle of the lines also. for example:: select * from table1; -- getting... (5 Replies)
Discussion started by: neelmani
5 Replies

6. Shell Programming and Scripting

Moving a line to the end of the file

I have a file with different directories in it. I would need to move one line within the file to the end of the list. Also not there could be blank line in the middle of it. Example /vol/fs1 /vol/fs2 /vol/fs3 /vol/fs4 /vol/fs5 /vol/fs6 /vol/fs7 So I would need /vol/fs2... (3 Replies)
Discussion started by: bombcan
3 Replies

7. Shell Programming and Scripting

Help on shell script : syntax error at line 62: `end of file' unexpected

Hi All, I have written a korn script (code pasted below). It is giving the error while debugging "new.sh: syntax error at line 62: `end of file' unexpected". I have re-written the whole code in VI and explored all help related to this error on this Unix forum and tried it. Somehow, I could... (7 Replies)
Discussion started by: schandrakar1
7 Replies

8. UNIX for Dummies Questions & Answers

stripping last character from end of each line

Hi, sorry for being dumb but I have a file with a variable amount of records (day to day it differs) but is fixed at 80 characters wide. Unfortunately the 80th and last charachter in each line is a "^M" carriage return character which i want to get rid of. Is there a really easy command that i can... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

9. Shell Programming and Scripting

end of the line character

How do i determine what the end of the line character is in a text file. for eg. is it \n or \f or \t etc.. Is there a unix command for this? (5 Replies)
Discussion started by: zomboo
5 Replies

10. UNIX for Dummies Questions & Answers

End of line character in BAsh

I'm trying to match the end of line character in a shell script under bash. What I have done it got is a loop that reads each line of a file into a temp variable and then I get each letter of that variable and do some work on it. One think I want to do it check if the character I checking if the... (1 Reply)
Discussion started by: Rukshan
1 Replies
Login or Register to Ask a Question