0

INPUT:

fofo jojo tst

fojo jofo sts

rhr hrhh dodo

  jojo hoho jojo

  zozo roro vovo

OUTPUT:

fofo jojo tst

fojo jofo sts

rhr hrhh dodo

jojo hoho jojo

zozo roro popo

NOTE: Please help me, I need to shift all rows, which have first column empty. Every fields are tab delimited. In this file some rows start from first column, but some rows start from second or third column. Thank you

Vonton
  • 2,872
  • 4
  • 20
  • 27

2 Answers2

3

You can do:

awk '{$1=$1}1' file
fofo jojo tst

fojo jofo sts

rhr hrhh dodo

jojo hoho jojo

zozo roro vovo

You can also use awk '$1=$1', but this will also remove empty lines and lines starting with 0


If file is tab separated:

cat file
fofo    jojo    tst

fojo    jofo    sts

rhr     hrhh    dodo

        jojo    hoho    jojo

        zozo    roro    vovo

awk '{$1=$1}1' OFS="\t" file
fofo    jojo    tst

fojo    jofo    sts

rhr     hrhh    dodo

jojo    hoho    jojo

zozo    roro    vovo

This will remove any space/tabs in front of first data without change formating of file:

awk '{sub(/^[[:space:]]*/,"")}1' file
fofo    jojo    tst

fojo    jofo    sts

rhr     hrhh    dodo

jojo    hoho    jojo

zozo    roro    vovo
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • 1
    the `{$1=$1}` (no matter with/without `OFS=\t`) will "reformat" the line, that is `a\tb\t\t\t\tc` will became `a\tb\tc` . this could be problem. at least OP didn't mention that needs. – Kent Jun 11 '14 at 09:21
  • @kent If I uses this data `a\tb\t\t\t\tc` I get this ouput from my `gnu awk` `a\tb\t\t\t\tc`, so this is not true in at least my case. – Jotne Jun 11 '14 at 09:26
  • I just tested with your `awk '{$1=$1}1' OFS="\t" file` a line in file like `abc` will become `abc` here. I hope I didn't do anything wrong. – Kent Jun 11 '14 at 09:29
  • @Kent Then I do understand you, I thought you ment it rmoved the characters `\t` not the tabs. But yes, it reformat the file, so if that is a problem, its best to use my last `awk` that will do the same as your `sed` – Jotne Jun 11 '14 at 09:34
3

like this

sed 's/^\s*//' file

or I didn't understand the requirement right?

Kent
  • 189,393
  • 32
  • 233
  • 301