-2

This is NOT a duplicate of another question. All previous questions/solutions posted on stackoverflow have got the same issue: additional spaces get replaced into a single space.

Example (1.txt)

filename Nospaces filename One space filename Two spaces filename Three spaces 

Result:

awk '{$1="";$0=$0;$1=$1}1' 1.txt One space Two spaces Three spaces awk '{$1=""; print substr($0,2)}' 1.txt One space Two spaces Three spaces 
3
  • @hek2mgl This is NOT a duplicate of another question. All previous questions/solutions posted on stackoverflow have got the same issue: additional spaces get replaced into a single space. Commented Mar 30, 2016 at 13:57
  • not all of them have that issue. See the answers to stackoverflow.com/q/29514679/1745001 for example. Commented Mar 30, 2016 at 14:51
  • Hang on - that was YOUR question! You accepted the right answer almost exactly a year ago and now you're back asking the same question again. What's going on? Commented Mar 30, 2016 at 14:59

5 Answers 5

2

Specify IFS with -F option to avoid omitting multiple space by awk

awk -F "[ ]" '{$1="";$0=$0;$1=$1}1' 1.txt awk -F "[ ]" '{$1=""; print substr($0,2)}' 1.txt 
Sign up to request clarification or add additional context in comments.

5 Comments

works great. just wondering what are the caveats
Hmm now the problem is if I want to start fro mthe second column by using $1=$2=""
This works for even for nth column.
no it doesnt as there are multiple spaces in later columns
Edit your question to show the expected output.
2

If you define a field as any number of non-space characters followed by any number of space characters, then you can remove the first N like this:

$ sed -E 's/([^[:space:]]+[[:space:]]*){1}//' file Nospaces One space Two spaces Three spaces 

Change {1} to {N}, where N is the number of fields to remove. If you only want to remove 1 field from the start, then you can remove the {1} entirely (as well as the parentheses which are used to create a group):

sed -E 's/[^[:space:]]+[[:space:]]*//' file 

Some versions of sed (e.g. GNU sed) allow you to use the shorthand:

sed -E 's/(\S+\s*){1}//' file 

If there may be some white space at the start of the line, you can add a \s* (or [[:space:]]*) to the start of the pattern, outside of the group:

sed -E 's/\s*(\S+\s*){1}//' file 

The problem with using awk is that whenever you touch any of the fields on given record, the entire record is reformatted, causing each field to be separated by OFS (the Output Field Separator), which is a single space by default. You could use awk with sub if you wanted but since this is a simple substitution, sed is the right tool for the job.

2 Comments

this doesnt work for the n-th column (2nd or 3rd)
"doesn't work" isn't a very clear description of the problem. Anyway, I changed it slightly (use a * instead of a +). I guess that it should do what you expect now.
1

To preserve whitespace in awk, you'll have to use regular expression substitutions or use substrings. As soon as you start modifying individual fields, awk has to recalculate $0 using the defined (or implicit) OFS.

Referencing Tom's sed answer:

awk '{sub(/^([^[:blank:]]+[[:blank:]]+){1}/, "", $0); print}' 1.txt 

6 Comments

this code doesnt work
It's fine with GNU awk. What awk are you using?
With that old version, use gawk --re-interval '...'
yup, works. but shorter, no subs needed already posted as an answer
one issue, if one of th elines contains less columns than the defined n-th column, it gets printed. fix: awk '{for(i=0;i<[column_id];i++)sub(/[^[:space:]]+[[:space:]]*/,"")}1' Just wondering how does :space: differ from :blank: in this case
|
1

Use cut:

cut -d' ' -f2- a.txt 

prints all columns from the second to the last and preserves whitespace.

6 Comments

please read the question first
@meso_2600 Uh sorry, I tough you mean rows. I would use cut. Updated the answer.
and again. please read the question, it is awk or sed ;)
I read it. However, cut is obviously the right tool for the job. Usually asking something like "do this with tool A or B" is frowned upon because you already suggesting a probably wrong solution in the question. Why are you forced to use awk or sed?
@meso_2600 I cannot reproduce that. The example above shows how to print the 2nd to last column. If your "real" problem differs from the one described in the question, how should I give the right answer then?
|
-1

Working code in awk, no leading space, supporting multiple space in the columns and printing from the n-th column:

awk '{ print substr($0, index($0,$column_id)) }' 1.txt 

7 Comments

this is just childish downvoting proper answer :)
Good answer. Potential downfall: same content in two different columns.
please share example as I can't reproduce such issue
ok i can see the issue. thanks. your answer is correct
For others: echo foo bar baz bar | awk -v column_id=4 '{print substr($0, index($0,$column_id))}' will print too many columns
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.