I've asked a question before on true row deletion, and got some great answers, but I'm now interested to know how you would remove all the rows below a specific row number from one specific column. All other columns and rows should remain intact.

As a bonus how would you also then paste/print another column into the specific deleted space

Example (columns are tab delimited ) - remove line 2 and all lines below it only in column 1, then shifting one column left move columns 2 and 3 from line 2 down:


hunkey dorey pringle  
jungley fevery jingles  
freshly cutting jackle  
Queen Kingly Squire


hunkey dorey pringle  
fevery jingles  
cutting jackle  
Kingly Squire
sed 'N,$s/^[^ ]\+[ ]\+//'

where N is the line number of the fist line to lose its first column.

  • This seems to work in this exact example, but when run on a larger scale it seems to delete everything below the line number given across all the columns. However in this case the file im using is already empty in column from said line and i'm merely wishing to slide the column lines to the right into the spaces place. – Giles May 28 '16 at 18:35
  • 1
    Just a matter of using the right regular expressions int the substitutions. The regexp given looks for any number of non-spaces followed by any number of spaces, 'sed 'N,$s/^[^ ]\+[ ]\+//'' will look for one or more non-spaces followed by one or more spaces (edited answer) – xenoid May 28 '16 at 19:19

awk solution.

awk '{if(NR>1){$1="";sub(" ","")}}1'
  • 1
    This works very well – Giles May 28 '16 at 18:45

With GNU sed :

sed '2,$ s/[^\t]*\t//' file

Portable :

tab=$(printf '\t')
sed '2,$ s/[^'"$tab"']*'"$tab"'//' file
  • 2,$ from the second line to the last line
  • s/[^\t]*\t// substitute any character except the tab character to the next tab (the first tab) with nothing.
  • for me neither of these example solutions seem to work for the example file. – Giles May 28 '16 at 18:43
  • I suspect the file isn't tab delimited as a requirement in your original post. – fd0 May 28 '16 at 18:48
  • I've checked the txt file and its definitely tab delimited. – Giles May 28 '16 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.