problem with new line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with new line
# 1  
Old 04-03-2006
problem with new line

Hi Masters,

Here is the code someone posted days ago, but it didn't work well because it did't omit the newline character when $2 conbines. Can someone help me out?

awk '{if($1==prev) printf(" /// "$2); else {printf("\n"$1" " $2);prev=$1}}'

Basically, this code is trying to remove the redundent line in a two column spreadsheet file based on the field one, but merge field 2 if field one is redundent.

Thanks in advance.
# 2  
Old 04-04-2006
Need the sample text again please.
I think your thread was removed in violation of Rule Number 6.
# 3  
Old 04-04-2006
Thanks! You are right. So I am fraid of violating rules again. But the basic idea is,

H82421 RDH11
H82421 DKFZP564M1462
H82435 BAGE4
H82435 MLL3
H82527 C20orf17
H82532 -
H82706 ID2
H82801 -
H82812 TNRC6B
H82872 UACA

What I need,

H82421 RDH11 /// DKFZP564M1462
H82435 BAGE4 /// MLL3
H82527 C20orf17
H82532 -
H82706 ID2
H82801 -
H82812 TNRC6B
H82872 UACA

Your code gave the line break in between $2,
H82421 RDH11^M /// DKFZP564M1462
H82435 BAGE4^M /// MLL3
H82527 C20orf17
H82532 -
H82706 ID2
H82801 -
H82812 TNRC6B
H82872 UACA

I could use some text editor to remove line breaks, but thought the complete code could be helpful for other people too. In addition, if the redundent lines are not sorted, what code should be? Thanks so much.
# 4  
Old 04-04-2006
Are you by any chance viewing the output file in DOS/Windows?
"^M" normally appears when you do so..as these OSes do not recognize new lines(UNIX) properly
# 5  
Old 04-04-2006
Try this

awk 'BEGIN{x="";i=0} NR == 1 {x=$1;y=$0} NR > 1 && x == $1 {printf("%s /// %s\n",y,$2);i=NR+1} NR == i {x=$1;y=$0} NR > i && x != $1 {printf("%s\n",y);x=$1;y=$0} END {printf("%s\n",y);}'
# 6  
Old 04-04-2006
Thank you all. Both of your code generated almost same output in terms of line breaks, second code didn't work with multiple redundent lines.

First output,
AA001897 SPTA1
AA001918 MI-ER1^M /// ER1^M /// KIAA1610^M /// DKFZp781G0451
AA001924 Cep63^M /// FLJ13386
AA001950 KIF13A
AA001952 -
AA001970 -
AA001976 -

Second output,
AA001897 SPTA1
AA001918 MI-ER1^M /// ER1
AA001918 MI-ER1^M /// KIAA1610
AA001918 MI-ER1^M /// DKFZp781G0451
AA001924 Cep63^M /// FLJ13386
AA001950 KIF13A
AA001952 -
AA001970 -
AA001976 -
AA002006 -

I just wonder if this is my os problem or not. I am using Mac OS X, and bash shell.
# 7  
Old 04-05-2006
It probably could be a OS problem. You could check by looking for special charcters by using "cat -vet" on the output file. Regarding your question on what do we do if the input is unsorted wrt to the first column, we sort it first using:

sort -t' ' -k 1 testfile.txt

and pipe the output to the awk statement you used.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problem with traverse through line

i have a file like below New_file.txt 123|345|ab cd|ef gh 345|456|jk hu|uyh My script is #!/bin/ksh set -x for line in `cat New_file.txt` do a1=`echo $line|cut -d '|' -f1` echo $a1 done output ++ cat New_file.txt (2 Replies)
Discussion started by: Rajesh_us
2 Replies

2. Shell Programming and Scripting

Line Break problem

Hi All, Please can you advise/help on the below issue i did a bcp out of a table, it is having problem of line break such that one line is getting broken in two lines for many records. eg Correct format Line 1: - 000f00000bfe2c2c 000218310300000000GBP GBP 734654 10970.35 ... (3 Replies)
Discussion started by: mad_man12
3 Replies

3. Shell Programming and Scripting

Delete line with match and previous line quoting/escaping problem

Hi folks, I've list of LDAP records in this format: cat cmmac.export.tmp2 dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc cmmac: 00:13:11:36:a5:06 dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc cmmac:... (4 Replies)
Discussion started by: tomas.polak
4 Replies

4. Shell Programming and Scripting

problem in seeing a lengthy line

hi , i need to check the first line of a tilde(~) delimitted source file, so i wrote a command as follows head -1 <source filename> but the problem is that the line is too lengthy,as it spans out of the window,so im not able to see the line till end.can anybody please suggest a workaround... (2 Replies)
Discussion started by: angel12345
2 Replies

5. Shell Programming and Scripting

Problem with read line

Hi everybody, I am new to shell script. Please help me with this problem. I have a file test.txt with the content like this: I have a shell script test.sh like this #!/bin/sh while read line do echo $line >> out.txt done < test.txt out.txt is expected to have the same content... (10 Replies)
Discussion started by: Dark2Bright
10 Replies

6. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

7. Shell Programming and Scripting

I need suggestion on problem read a file line by line and do stuff

At first, give my best wish for all MOD and admins here. I"m learning bash shell program just for a month, not too much, not too little, but i must admire that i'm very bad at math and algorithm. :( I want to do this : Read the content of a file line by line and at each line, ask me want to... (3 Replies)
Discussion started by: madi3d8
3 Replies

8. Shell Programming and Scripting

Problem with reading file line-by-line, and outputting to a new file

Hi everyone. I realise this is probably a bit of a noob question, but I'm actually a C# developer working on a legacy system, and can't remember much unix. I want to read from a pipe-delimeted file like formatted thusly: idno|PRODUCT|Name|street town postcode|etc|etc|etc|etc... (4 Replies)
Discussion started by: Darkness Fish
4 Replies

9. Shell Programming and Scripting

$line command problem

Hi all! I have a problem with line command. The aim is to write each line of stdin into a text file. For example with this command: $find / /usr/share/zoneinfo/WET /usr/share/zoneinfo/Zulu /usr/share/zoneinfo/iso3166.tab /usr/share/zoneinfo/localtime /usr/share/zoneinfo/zone.tab... (3 Replies)
Discussion started by: victorin
3 Replies

10. UNIX for Dummies Questions & Answers

Sed new line problem

Hi all, I've searched the web and this forum for this but not had any luck. I'm trying to use sed so when it finds a space it will insert a new line. What i have is a file containing .e.g 1 2 4 7 9 and want it to look like 1 2 4 7 9 I've tried: more test2 | sed 's/ /\\n/g'... (1 Reply)
Discussion started by: Cordially
1 Replies
Login or Register to Ask a Question