Append characters to end of line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Append characters to end of line
# 1  
Old 04-21-2010
Lightbulb Append characters to end of line

Coding in unix shell script.
Hi All,
Please help


Thanks

Last edited by sam12345; 04-21-2010 at 08:47 PM.. Reason: ffffff
# 2  
Old 04-21-2010
Code:
#!/bin/sh

while IFS="" read LINE
do
        while [ "${#LINE}" -lt 100 ]
        do
                LINE="${LINE}0"
        done
        echo "${LINE}"
done

Code:
 ./hundred.sh < infile > outfile

# 3  
Old 04-21-2010
Hi.

You could try something like:

Code:
awk '{printf( "%s%0" 100-length "d\n", $0, 0)}' file > tmpfile

A test with 10 characters:
Code:
$ cat file1
12345
67
8910

$ awk '{printf( "%s%0" 10-length "d\n", $0, 0)}' file1     
1234500000
6700000000
8910000000

# 4  
Old 04-21-2010
Thanks Corona,

I basically copied the code.. it works please could you explain what is happening.

I mean to say what is
Code:
IFS="" read LINE

while [ "${#LINE}" -lt 100 ] 
do LINE="${LINE}0" 
done 
echo "${LINE}"


Thanks

Last edited by Scott; 04-21-2010 at 06:37 PM.. Reason: Please use code tags
# 5  
Old 04-22-2010
Quote:
Originally Posted by scottn
Code:
$ awk '{printf( "%s%0" 10-length "d\n", $0, 0)}' file1

Hi, can you please explain these format specifiers? and why if I remove the last 0 in the script, it is giving me error saying:
Quote:
awk: (FILENAME=- FNR=1) fatal: not enough arguments to satisfy format string
`%s%04d
'
^ ran out for this one
# 6  
Old 04-22-2010
Quote:
Originally Posted by sam12345
Thanks Corona,

I basically copied the code.. it works please could you explain what is happening.

I mean to say what is
Code:
# Read line into LINE.  Do not break on spaces.  (IFS is 'inter-field-seperator', defaults to all whitespace.)
IFS="" read LINE
        # Loop while the length of LINE is < 100 chars
        while [ "${#LINE}" -lt 100 ] 
        do
                # Add 0 to the end of LINE
                LINE="${LINE}0" 
        done
        # print the line.
        echo "${LINE}"

The awk solution is probably better however.
# 7  
Old 04-22-2010
Quote:
Originally Posted by royalibrahim
Hi, can you please explain these format specifiers? and why if I remove the last 0 in the script, it is giving me error saying:
Because it requires two values.

With an input of...
Code:
123
98765

awk '{printf( "%s%0" 10-length "d\n", $0, 0)}' file1
                          |            |  |
                       format          |  value 2
                                       |
                                    value 1
                                    
Record 1 (123):
%s using value 1 ($0)           evaluates to 123
10 - length                     evaluates to 7    (10 minus length of "123" (i.e. 3))
"%0" 10 - length ...  "d"       evaluates to "%07d"
%07d using value 2 (0)          evaluates to    0000000
%s%07d                          evaluates to 1230000000

Record 2 (98765):
%s using value 1 ($0)           evaluates to 98765
10 - length                     evaluates to 5    (10 minus length of "98765" (i.e. 5))
"%0" 10 - length ...  "d"       evaluates to "%05d"
%05d using value 2 (0)          evaluates to      00000
%s%05d                          evaluates to 9876500000

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append Pipes at the end of each line

Hi , I have pipe delimited file containing 12 columns having below data in AIX Input : A|B|C|D|E|F|G|H|I|J|K|L^M A1|B1|C1|D1|E1^M A2|B2|C2^M So in first row i have 11 pipes which is fine. In second row I have 4 pipes, so additional 7 pipes should get append In third row I have 2... (3 Replies)
Discussion started by: sonu_pal
3 Replies

2. Shell Programming and Scripting

Append this string to end of each line

Platform: Solaris 10 I have a file like below $ cat languages.txt Spanish Norwegian English Persian German Portugese Chinese Korean Hindi Malayalam Bengali Italian Greek Arabic I want to append the string " is a great language" at end of each line in this file. (3 Replies)
Discussion started by: omega3
3 Replies

3. Shell Programming and Scripting

Help Needed! - Cut characters after a text string and append to end of filename

Hi all.. I have several unique files that contain one thing in common, and that is acct#. For all files in the directory, I want to append the 10 characters following the word "ACCOUNT:" to the end of the filename. for example: I have file 111_123 that contains ACCOUNT:ABC1234567 The file... (5 Replies)
Discussion started by: cinderella1
5 Replies

4. Shell Programming and Scripting

find a certain line and append text to the end of the line

After I create printer queues in AIX, I have to append a filter file location within that printers custom file. within lets say test_queue.txt I need to find the row that starts with :699 and then I need to append on the end the string /usr/local/bin/k_portrait.sh. Now I've gotten the sed... (2 Replies)
Discussion started by: peachclift
2 Replies

5. Shell Programming and Scripting

Append text to end of every line

I've scoured the internet with mixed results. As an amateur I turn to the great minds here. I have a text file of 80 or so lines. I want to add ".pdf" to the end of each line. (For now that's it) Most of the internet points toward using "sed". I don't know coding but can figure things out... (4 Replies)
Discussion started by: spacebase
4 Replies

6. Shell Programming and Scripting

Append text to end of line on all lines

Hi, I've spent some time researching for this but can't seem to find a solution. I have a file like this 1234|Test|20101111|18:00|19:00There will be multiple lines in the file with the same kind of format. For every line I need to make it this 1234|Test|20101111|18:00|19:00||create... (5 Replies)
Discussion started by: giles.cardew
5 Replies

7. Shell Programming and Scripting

append end of line with 8 spaces

child_amt=$amount prev_line="$prev_line $child_amt" i am getting the result like this 21234567890001343 000001004OLFXXX029100020091112 0000060 but i want 8 spaces between the eg: 21234567890001343 000001004OLFXXX029100020091112 0000060 how can i do this in .ksh (1 Reply)
Discussion started by: kshuser
1 Replies

8. Shell Programming and Scripting

how to append the pattern at the end of the line

-Hi I have multiple files which contain a line with the word "exec". I need to add the following pattern " -cmode -ccheap" on the same line where "exec" is at the end. Any idea? Thanks a lot in advance to everybody... -A (2 Replies)
Discussion started by: aoussenko
2 Replies

9. Shell Programming and Scripting

append a character at end of each line of a file

Hi, i want to append a character '|' at end of each line of a file abc.txt. for example if the file abc.txt conatins: a|b|c 1|2|33 w|2|11 i want result file xyz.txt a|b|c| 1|2|33| w|2|11| I know this is simple but sumhow i am not able to reach end of line. its urgent, thanks for... (4 Replies)
Discussion started by: muaz
4 Replies

10. Shell Programming and Scripting

How to append words at the end of first line

Hi Unix gurus This is my first post here. I have a file which looks like this: string1,string2 ,string3 ,string4 ... ... I need to append all words below first line to the first line, ie string1,string2,string3,string4,... Bear in mind that it is not known how many lines follow... (11 Replies)
Discussion started by: lmatlebyane
11 Replies
Login or Register to Ask a Question