Awk while-loop printing extra character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk while-loop printing extra character
# 1  
Old 08-05-2011
Awk while-loop printing extra character

Hi, I'm using a while-loop in an awk script. If it matches a regular expression, it prints a line. Unfortunately, each line that is printed in this loop is followed by an extra character, "1".

While-statement extracted from my script:
Code:
getline temp;
while (temp ~ /.* x[0-9][0-9][0-9][0-9] .*/) print temp getline temp;

Sample text from original text file:
Code:
x01334
x01432
t-4321

Outputted text:
Code:
x013341
x014321

Does anyone know why this is happening? Any help would be appreciated, thanks
# 2  
Old 08-05-2011
Hi,

What is the context of that piece of code?

I think you are sending to print two parts:
'temp' -> which has the line
'getline temp' -> which returns success (the number 1).

and the 'print' function joins them and send it to output.

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 08-06-2011
Continuing the observation by birei, all you should need to do is to add a semicolon and some curly braces.

Messy
Code:
while (temp ~ /.* x[0-9][0-9][0-9][0-9] .*/){ print temp;  getline temp; }

Better
Code:
while (temp ~ /.* x[0-9][0-9][0-9][0-9] .*/) 
{
   print temp;
   getline temp;
}

This User Gave Thanks to agama For This Post:
# 4  
Old 08-06-2011
Thank-you Agama and Birei, you two are correct Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: printing column using for loop

Hello: I've input data: Input data --- 3:60069:C:T 60069 C T 1 0 0 1 0 0 1 0 0 1 0 0 1 --- 3:60079:A:G 60079 A G 1 0 0 0.988 0.012 0 1 0 0 1 0 0 1 --- rs186476240:60157:G:A 60157 G A 1 0 0 1 0 0 1 0 0 1 0 0 1 I edit/make first few columns before numbers (6th column) and want to... (4 Replies)
Discussion started by: genome
4 Replies

2. Shell Programming and Scripting

awk - Removing extra character when setting variable

I have a data file d0 that looks like this: $cat d0 server1 running -n-cv- 8G 3.1% 1435d 15h server2 running -n---- 8G 39% 660d 22h server3 running -n--v- 8G 2.5% 1173d 6h server4 running -n---- 8G 1.1% 1048d 20h... (2 Replies)
Discussion started by: jake0391S
2 Replies

3. Shell Programming and Scripting

Character printing with AWK

I was wondering if I could use AWK to print from the nth character position to nth for all records in the file. All I have been doing is printing/copying fields rather than characters. I found a way to print lines greater/less/equal to a character length which led me to believe there is a way... (3 Replies)
Discussion started by: MIA651
3 Replies

4. Shell Programming and Scripting

printing only columns containing a determined character with awk

Hello I'm trying to extract two columns from a database using awk, the thing is i have a variable number of columns in each row, and I just need to get out the two first ones ending in "," This is an example: ABE, ABE V149 MAZIE ARD CYN ACY, ACY, , , ,TEC, , 5000, , , 1, ZNY,ZDC ABE, ABE... (1 Reply)
Discussion started by: vir
1 Replies

5. Shell Programming and Scripting

extra character with iconv encoding

hey, I am trying to convert a sample russian encoding file to English encoding using iconv utility. Its almost done but with each converted character i am getting one extra character which must not come. my sample Russian text is test.txt А Б В Г Д Е Ж З И Й К ~ and script which i... (4 Replies)
Discussion started by: peeyushgehlot
4 Replies

6. Shell Programming and Scripting

Remove extra character

Hi I am using cat <filename> command in one of my datastage job(Command Activity). It is giving actual value but giving extra line. Eg: Displayed Output: 1 and showing extraline(Eg: 1 ) I had checked even wc -c it is giving one character extra. If the file contains 11. wc -c says 3. ... (3 Replies)
Discussion started by: cnrj
3 Replies

7. Shell Programming and Scripting

how to delete extra character in a line?

And I want to delete the characters longer than 20 for each line start with #. The other lines should remain the same. I think this can be done by sed. Could anyone help me with this? Thanks! my input file: #ZP_05494889.1_Clostridium_papyrosolvens... (3 Replies)
Discussion started by: ritacc
3 Replies

8. Shell Programming and Scripting

sort file adding extra character

HI all i have this script : #!/bin/bash sort /usr/tmp/"REPORT"$1 -o \ /usr/tmp/"SREPORT"$1 -k 1,7 -S 150 end of script now i'm doing this command : ls -lsgt *REPORT* 4 -rw-r--r-- 300 Sep 16 REPORT54784 4 -rw-r--r-- 301 Sep 16 SREPORT54784 as you can see the sorted file... (5 Replies)
Discussion started by: naamas03
5 Replies

9. Shell Programming and Scripting

printing with awk through while loop

ive input file contains to clums a and b spreated by pipe a | b 123|456 323|455 and xyz contains other info about a and b now i want to print as follows: a | b | "info from xyz" but "info from xyz" might be more than 1 line and i want to keep the format to 3 cloums. how to do it?... (3 Replies)
Discussion started by: windows
3 Replies

10. UNIX for Dummies Questions & Answers

Silly question on printing for loop in AWK

Hi, One silly question. I would like to add statement like below and append to a file. I used the below code; however, it does not work. Can anyone please tell me what mistakes I have made? awk ' { for (i=1;i<=563;i++) print i }'>>output.txt Thanks. -Jason (1 Reply)
Discussion started by: ahjiefreak
1 Replies
Login or Register to Ask a Question