LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-21-2011, 09:53 PM   #1
captainentropy
Member
 
Registered: Mar 2010
Location: Berkeley
Distribution: Ubuntu, Mint, CentOS
Posts: 81

Rep: Reputation: 0
prevent awk from inserting new line


Hi,

I can't figure this one out.

I have a file with three columns. I need to add some fixed value to one and move another to a new position. e.g.:

30850 chr1 9896886
60 chr1 9896887
11937 chr1 22105724
601 chr1 27242859
128 chr1 27242861
77 chr1 33451497
62 chr1 33451499
9743 chr1 37695593
9632 chr1 37695595
9551 chr1 38408988

needs to become this:
chr1 9896886 9896891 30850
chr1 9896887 9896892 60
chr1 22105724 22105729 11937
chr1 27242859 27242864 601
chr1 27242861 27242866 128
chr1 33451497 33451502 77
chr1 33451499 33451504 62
chr1 37695593 37695598 9743
chr1 37695595 37695600 9632
chr1 38408988 38408993 9551

but this command: awk '{print $2,$3,$3+5,$1}' file_in > file_out results in this:

chr1 9896886
9896891 30850
chr1 9896887
9896892 60
chr1 22105724
22105729 11937
chr1 27242859
27242864 601
chr1 27242861
27242866 128
chr1 33451497
33451502 77
chr1 33451499
33451504 62
chr1 37695593
37695598 9743
chr1 37695595
37695600 9632
chr1 38408988
38408993 9551

What am I doing wrong?! Perhaps it's a weird format in the original file but I can't tell.
 
Old 09-21-2011, 10:09 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
I suspect that you have a carriage return in addition to the linefeed at the end of the line. Try this:
Code:
awk '{sub(/\r/,"",$0);print $2,$3,$3+5,$1}' file_in > file_out
The 'sub' will strip out any carriage returns in the input record.
 
Old 09-22-2011, 12:35 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Or you can set the RS to include the \r if you need to do this regularly.
 
Old 09-22-2011, 04:13 PM   #4
captainentropy
Member
 
Registered: Mar 2010
Location: Berkeley
Distribution: Ubuntu, Mint, CentOS
Posts: 81

Original Poster
Rep: Reputation: 0
d'oh! There was a carriage retun there?! Dumb thing to overlook.

@macemoneta your command work perfectly. I also see that the sub command works the same as sed 's/\r//g' then I can pipe it to the awk print command.
@grail I didn't known about RS, but I got this to do the same thing: awk 'BEGIN {RS="\r"} {print $2,$3,$3+5,$1}'

Thanks everyone!

Last edited by captainentropy; 09-22-2011 at 04:18 PM.
 
Old 09-22-2011, 10:00 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
To be on the safe side you could do something like:
Code:
RS="[\r\n]+"
So then you are covered for either file type.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 11:48 AM
inserting some text into a file using 'awk' command eliote Programming 16 10-26-2010 04:16 AM
Inserting images in OpenOffice.org - how to prevent scaling? klearview Linux - Software 2 10-09-2010 12:52 AM
Inserting spaces using SED/AWK ? aravindts Programming 4 09-29-2006 06:28 AM
Inserting a carriage return in awk legtester Linux - General 1 08-17-2003 06:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration