awk one liner to print to end of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk one liner to print to end of line
# 1  
Old 01-06-2013
awk one liner to print to end of line

Given:

Code:
1,2,whatever,a,940,sot

how can i print from one particular field to the end of line?

Code:
awk -F"," '{print $2 - endofline}'

the delimiter just happens to be a comma "," in this case. in other cases, it could be hypens:

Code:
1---2---whatever---a---940---sot

# 2  
Old 01-06-2013
Code:
# example with three -> end
awk '{ for(i=3; i<=NF; i++) {printf( (i==NF)?"%s\n" : "%s ", $i) } somefile

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 01-06-2013
jim mcnamara's proposal needs minor corrections:
Code:
$ awk '{ for(i=3; i<=NF; i++) {printf( (i==NF)?"%s\n" : "%s---", $i) }}' FS="---" file
whatever---a---940---sot

You may also want to give this a try:
Code:
$ awk '{sub($1FS$2FS,"")}1' FS="---" OFS="---" file
whatever---a---940---sot

This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-06-2013
Code:
echo "one,two,three,four,five" | awk -F, '{ for (x=nr; x<NF; x++) {printf $x ","} print $NF }' nr=3
three,four,five

Code:
echo "one---two---three---four---five" | awk -F-+ '{ for (x=nr; x<NF; x++) {printf $x "---"} print $NF }' nr=3
three---four---five


Last edited by Jotne; 01-06-2013 at 06:02 AM..
This User Gave Thanks to Jotne For This Post:
# 5  
Old 01-06-2013
Code:
awk -F, '{s=$f; for(i=f+1; i<=NF; i++) s=s FS $i; print s}' f=2

Code:
sed 's/[^,]*,*/§/2; s/.*§//'

if there is just a single character as field separator:
Code:
cut -d, -f2-

--
@ jotne, if printf is used without explicitly specifying the format field than this is left to the input data.. Try replacing one of the fields with "%s" for example

Last edited by Scrutinizer; 01-06-2013 at 07:17 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract values and print at end of each line

In the below perl I am trying to extract and print the values AF1=, the GT value, and F or QUAL diveded by 33 (rounded to the nearest whole #). The GT value is at the end after the GT:PL so all the possibilities are read into a hash h, then depending on the value that is in the line the... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. 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

3. UNIX for Dummies Questions & Answers

Any awk one liner to print df output?

Hi, OS = Solaris Can anyone advise if there is a one liner to print specific output from a df -k output? Running df from a command line, it sometimes gives me 2 lines for some volume. By re-directing the output to a file, it always gives 1 line for each. Below is an example output,... (4 Replies)
Discussion started by: newbie_01
4 Replies

4. Shell Programming and Scripting

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: $ cat myfile | awk '{if ( $0 != "" ) print $0"\;\;\;\;0\;1"}' Result:... (5 Replies)
Discussion started by: regisl67
5 Replies

5. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

6. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

7. Shell Programming and Scripting

awk multiple-line search and replace one-liner

Hi I am trying to search and replace a multi line pattern in a php file using awk. The pattern starts with <div id="navbar"> and ends with </div> and spans over an unknown number of lines. I need the command to be a one liner. I use the "record separator" like this : awk -v... (8 Replies)
Discussion started by: louisJ
8 Replies

8. Shell Programming and Scripting

How to Print from matching word to end using awk

Team, Could some one help me in Printing from matching word to end using awk For ex: Input: I am tester for now I am tester yesterday I am tester tomorrow O/p tester for now tester yesterday tester tomorrow i.e Starting from tester till end of sentence (5 Replies)
Discussion started by: mallak
5 Replies

9. UNIX and Linux Applications

Print date at END clause of AWK

Hi to all! I 'm new in unix programing so... may be I decided a wrong tool to solve the problem but anyway... all road goes to rome jajaja. My question is: There is any way to print date at the END clause of an AWK script. I mean, I'm writing a tool with AWK and the results are redirected to a... (4 Replies)
Discussion started by: fmeriles
4 Replies

10. Shell Programming and Scripting

Print starting 3rd line until end of the file.

Hi, I want to Print starting 3rd line until end of the file. Pls let me know the command. Thanks in advance. (1 Reply)
Discussion started by: smc3
1 Replies
Login or Register to Ask a Question