replace lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace lines in a file
# 1  
Old 03-16-2009
Question replace lines in a file

I've got a file full of numbers, example:

Code:
cat test.file

60835287
0
51758036
40242437
0
32737144
0
24179513
0
4131489957

I want to replace those numbers

Code:
TMP2="/root/test.file"
for number in `cat $TMP2` ;do
     calc=$(awk -v a="$number" 'BEGIN{printf "%.2f\n GB",  a/1024/1024}')
     sed -i 's/$number/$calc/g' $TMP2
done

or

Code:
TMP2="/root/test.file"
for number in `cat $TMP2` ;do
     calc=$(awk -v a="$number" 'BEGIN{printf "%.2f\n GB",  a/1024/1024}')
     sed -e 's/$number/$calc/g' > test.txt
done

But it doesn't work at all, what is the problem? I'd guess it's something to do with the variables and sed, like it doesn't read them..

I've also tryd putting the sed command or variables into double quotes (sed -e "s/$number/$calc/g" > test.txt ) but that doesn't help either!
# 2  
Old 03-16-2009
This will replace the original file. For just testing, remark the 2nd line.
Code:
awk '{printf "%.2f GB\n",  ($1/1024/1024)}' infile > infile.tmp
mv infile.tmp infile

# 3  
Old 03-16-2009
Quote:
Originally Posted by zaxxon
This will replace the original file. For just testing, remark the 2nd line.
Code:
awk '{printf "%.2f GB\n",  ($1/1024/1024)}' infile > infile.tmp
mv infile.tmp infile

This does indeed solve the problem but only for the example I gave, your solution would calculate and replace ALL the numbers into GB and that's fine if the user does specify the unit, however in another part of my script I choose the unit automatically (MB/GB/TB) therefor I need a solution to replace the numbers one by one (each different size unit) and not file wide!

Example:

Code:
          for number in `cat $TMP2` ;do
            if (($number>=1024000000)); then
              CALC=$(awk -v a="$number" 'BEGIN{printf "%.2f\n TB",  a/1024/1024/1024}')
            elif (($$number>=1024000)); then
              CALC=$(awk -v a="$number" 'BEGIN{printf "%.2f\n GB",  a/1024/1024}')
            elif (($number<1024000)); then
              CALC=$(awk -v a="$number" 'BEGIN{printf "%.2f\n MB",  a/1024}')
            fi
            sed -i 's/$number/$CALC/g' $TMP2
          done

Only the sed part that is actually suppose to replace the number doesn't work.

Last edited by TehOne; 03-16-2009 at 01:31 PM..
# 4  
Old 03-16-2009
Oh and using:

Code:
sed -i "s/$number/$CALC/g" $TMP2

gives this error:

Code:
sed: -e expression #1, char 8: unterminated `s' command

# 5  
Old 03-17-2009
The more information you give, the more accurate the answer can hopefully be Smilie

Since awk works on every line of a file anyway (but the BEGIN and END part), there is no need to wrap it into a for-loop.
You can write all this in awk. Also you don't have to give the number via shell variable to awk. awk can just parse $0 (the whole line) or $1 (the 1st field).

For that sed line, which is not needed but as info: To let the shell explicit know where a variable starts and ends you should write them like this:
Code:
${var}

Also the output of awk will be the wanted output so you don't have to change it again with sed. This is redundant.

Stripping out redundancy etc, your script should maybe look more like:
Code:
$> cat mach.ksh
awk '
        {
                if ( $1 >= 1024000000 ) { printf "%.2f TB\n",  $1/1024/1024/1024 }
                if ( $1 >= 1024000 ) { printf "%.2f GB\n",  $1/1024/1024 }
                if ( $1 < 1024000 ) { printf "%.2f MB\n",  $1/1024 }
        }
' infile
$> ./mach.ksh
58,02 GB
0,00 MB
49,36 GB
38,38 GB
0,00 MB
31,22 GB
0,00 MB
23,06 GB
0,00 MB
3,85 TB
3940,10 GB

The \n have been placed to the end of the lines since it looked somewhat strange.

Last edited by zaxxon; 03-17-2009 at 03:54 AM.. Reason: typos, i need coffee
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string with lines stored in a file

OS version: RHEL 6.7 Shell : Bash I have a file like below $ cat pattern.txt 'T_PKT_HEADER' 'T_ORD_ITM_LOYX' 'T_ORDERITM_TRMS' 'T_ORDER_ITEM' 'T_ORDER_ITM_PRI' 'T_ORDER_ITEM_OM' 'T_ORDER_ITEM_XA' 'T_ORDER_ATT' 'T_ORDER_ACTNSET' 'T_ORDER_XM' 'T_ORDER_X' 'T_ORDER_TNTX'... (7 Replies)
Discussion started by: kraljic
7 Replies

2. Shell Programming and Scripting

Grep 2 consecutive lines and replace the second line in a file

I have a file lake this cat ex1.txt </DISCOUNTS> <B2B_SPECIFICATION elem="0"> <B2B_SPECIFICATION elem="0"> <DESCR>Netti 2 </DESCR> <NUMBER>D02021507505</NUMBER> </B2B_SPECIFICATION> <B2B_SPECIFICATION elem="1"> <DESCR>Puhepaketti</DESCR>... (2 Replies)
Discussion started by: Dhoni
2 Replies

3. Shell Programming and Scripting

Replace first 3 characters in a unix file in all lines

Replace first 3 characters in a unix file (say replace "A&B" with "C&D") in all lines of the file. Need a sed or awk script to do this. Kindly help! -Kumar (4 Replies)
Discussion started by: vasan2815
4 Replies

4. Emergency UNIX and Linux Support

Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk... searched the forums but didn't find exact answer (9 Replies)
Discussion started by: greenworld123
9 Replies

5. Shell Programming and Scripting

sed to replace a line with 2 or more different lines in same file

i have something like this... cat filename.txt <complexType name="abc"> bklah vlah blah blha blha blah blha </complexType > <complexType name="def"> bklah vlah blah blha blha blah blha </complexType > . . .and so on.. its a very large file (11 Replies)
Discussion started by: vivek d r
11 Replies

6. Shell Programming and Scripting

Replace lines in a file

Hi everybody, I am a newbie in shell scripting and I'm trying to write a script which reads lines from a file, searching some of this lines to change a specified number. I want to replace the line for another in the file. I have to replace multiples lines, so I have a for. Now I am trying with... (1 Reply)
Discussion started by: alex82
1 Replies

7. Shell Programming and Scripting

Replace lines from one file to another

Hello, I have 8 lines containing these unique words in both files 645147468537 673962863160 673962864957 691717701950 707917019907 790085591726 792975507744 852174812753 file.dat.orig (has 1000 lines) and file.dat(has only 8 lines) I want to replace those lines in file.dat.orig by... (1 Reply)
Discussion started by: jakSun8
1 Replies

8. Shell Programming and Scripting

Replace whole lines in the file from different files

Hi all, I have two parameter blocks in a configuration file, file1.conf. First parameter block starts with PWAT-PARM1 and ends with END PWAT-PARM1. Second parameter block starts with PWAT-PARM2 and ends with END PWAT-PARM2 (please see below in file1.conf). file1.conf is fixed width... (8 Replies)
Discussion started by: Jae
8 Replies

9. Shell Programming and Scripting

replace multiple lines in file

Hello I am a beginner in shell script. I was trying to find a way to replace multiple lines of a file with different set of multiple line. sed -n '/begin/,/end/p' < sample1.txt >test.txt UNEDITED=`cat test.txt` vi test.txt EDITED=`cat test.txt`... (2 Replies)
Discussion started by: nox
2 Replies

10. Shell Programming and Scripting

Replace a perticular character of all lines of a file

Hi all, I am new to UNIX, so sorry if my question seem stupid to u. well i want to replace the first character of first 30 lines of a file, only if the first character is h. and in anothe script i want to replace a particular string/character say hello/h of a file.Condition: It should... (1 Reply)
Discussion started by: abovais
1 Replies
Login or Register to Ask a Question