strange: sed and awk print at end instead of begin of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting strange: sed and awk print at end instead of begin of line
# 1  
Old 10-17-2011
strange: sed and awk print at end instead of begin of line

Hi!
I have a strange behaviour from sed and awk, but I'm not sure, if I'm doing something wrong:

I have a list of words, where I want to add the following string at the end of each line:
\;\;\;\;0\;1

I try like this:
Code:
$ cat myfile | awk '{if ( $0 != "" ) print $0"\;\;\;\;0\;1"}'

Result: (it overwrites text at the beginning of the line)

Code:
;;;;0;1y
;;;;0;1hand
;;;;0;1occasion of
;;;;0;1other hand
;;;;0;1e
;;;;0;1ion

Same with sed ...

Am I doing something wrong? I use bash on Ubuntu 11.04.

Thanks for your help.
Regis
# 2  
Old 10-17-2011
See if this works for you:
Code:
sed 's#$#\\;\\;\\;\\;0\\;1#' File

# 3  
Old 10-17-2011
Code:
nawk 'NF{$0=$0 ";;;;0;1"}1' myFile
OR
sed '/..*/s#.*#&;;;;0;1#' myFile

# 4  
Old 10-18-2011
Thank you, for you replies. I learned a few things, but - it didn't help: still the same effect.
Smilie
I tried with
Code:
$ cat myfile | while read line ; do echo ${line}';;;;0;1' ; done

and even installed a different shell (ksh instead of bash) ... same effect.

I'll go to the office, and try on a different machine.
# 5  
Old 10-18-2011
Quote:
Originally Posted by regisl67
...
I try like this:
Code:
$ cat myfile | awk '{if ( $0 != "" ) print $0"\;\;\;\;0\;1"}'

Result: (it overwrites text at the beginning of the line)

Code:
;;;;0;1y
;;;;0;1hand
;;;;0;1occasion of
;;;;0;1other hand
;;;;0;1e
;;;;0;1ion

Same with sed ...
...
If you are on Unix, ensure that your file does not have Windows end-of-line characters. The output of the command:

Code:
od -bc myfile

should not show any "\r" characters.

Try fixing the end-of-line character (as shown below) before running your awk script on the file.

Code:
dos2unix myfile

# 6  
Old 10-18-2011
It must be a problem with my Ubuntu-installation at home:
I tried in the office on a Redhat system: works as expected, no problem!

$ od -bc myfile does NOT show any \r in the output.
However, I'll give a try with dos2unix when I'm back at home.

Thank you for your help! I appreciate to learn from all these answers!

---------- Post updated at 06:10 PM ---------- Previous update was at 09:50 AM ----------

On the office-Linux (Redhat), od -bc didn't show any \r ,
but at home, there where a lot of this characters.

$ dos2unix resolved this problem.

Thank you very much!

Kind regards - Regis
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print the string between 3rd and 4th backslashs to end of line

im trying to get awk to print the string between 3rd and 4th backslashs to end of line test could be any word this http://example.com/test/ >to this http://example.com/test/ > testalso the other way round insert string at end of line... (13 Replies)
Discussion started by: bob123
13 Replies

2. Shell Programming and Scripting

sed - remove begin of line up to the third and including occurence of character

hello. How to remove all characters in a line from first character ( a $ ) until and including the third occurrence of that character ( $ ). Any help is welcome. (10 Replies)
Discussion started by: jcdole
10 Replies

3. Shell Programming and Scripting

BEGIN and END format in awk

I'm new to awk, trying to understand the basics. I'm trying to reset the counter everytime the program gets a new file to check. I figured in the BEGIN part it would work, but it doesn't. #!/bin/awk -f BEGIN {counter=0} { sum=0 for ( i=1; i<=NF;... (1 Reply)
Discussion started by: guitarist684
1 Replies

4. Shell Programming and Scripting

Begin/End blocks in awk: confused

I am trying to understand how to use the END block in awk without much success. I have this script that I found: gawk '{count++; keyword = $1} if (count == 3) keyword = "order this" else print keyword " " k } }' << orderfile Is that the way that the END block should be used? I am... (6 Replies)
Discussion started by: newbie2010
6 Replies

5. Fedora

Shell Script - awk, begin, for and print

pointsb=`awk -v a2="$a2" -v b2="$b2" -v c2="$c2" -v yb="$yb" -v yc="$yc" \ 'BEGIN { for (y=yc; y<=yb; y++) { x = a2*y*y+b2*y+c2; print x, y }; }'` I am learning shell script. I was reading a script and got confused in this line. I understood that awk is allowing to assign the variable. But... (10 Replies)
Discussion started by: agriz
10 Replies

6. Shell Programming and Scripting

awk one liner to print to end of line

Given: 1,2,whatever,a,940,sot how can i print from one particular field to the end of line? awk -F"," '{print $2 - endofline}' the delimiter just happens to be a comma "," in this case. in other cases, it could be hypens: 1---2---whatever---a---940---sot (4 Replies)
Discussion started by: SkySmart
4 Replies

7. Shell Programming and Scripting

Use of Begin IF ,END IF END not working in the sql script

Hi I have written a script .The script runs properly if i write sql queries .But if i use PLSQL commands of BEGIN if end if , end ,then on running the script the comamds are getting printed on the prompt . Ex :temp.sql After connecting to the databse at the sql prompt i type... (1 Reply)
Discussion started by: isha_1
1 Replies

8. Shell Programming and Scripting

awk BEGIN END and string matching problem

Hi, Contents of BBS-list file: foo foo foo awk ' BEGIN { print "Analysis of \"foo\"" } /foo/ { ++n } END { print "\"foo\" appears", n, "times." }' BBS-list Output: Analysis of "foo" "foo" appears 3 times. awk ' (3 Replies)
Discussion started by: cola
3 Replies

9. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

10. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies
Login or Register to Ask a Question