Ela
Ela

Reputation: 419

Print all columns except first using AWK

I have a file which contains file list. The file looks like this

$ cat filelist
  D       src/layouts/PersonAccount-Person Account Layout.layout
  D       src/objects/Case Account-Record List.object

I want to cut first two Columns and print only file names with along directory path. This list is dynamic. File name has spaces in between. So I can't use space as delimiter. How to get this using AWK command?

The output should be like this

src/layouts/PersonAccount-Person Account Layout.layout
src/objects/Case Account-Record List.object

Upvotes: 0

Views: 7948

Answers (6)

Adam Katz
Adam Katz

Reputation: 16138

Here is a portable POSIX shell solution:

#!/bin/sh

cat "$@" |while read line; do
  echo "${line#* * }"
done

This loops over each line of the given input file(s) (or else standard input) and prints the line without the first two spaces or the text that exists before them. It is not greedy.

Unlike some of the other answers here, this will preserve spacing (if any) in the rest of the line.

If you want that as a one-liner:

while read L < filelist; do echo "${L#* * }"; done

This will fail if the uppermost directory's name starts with a space. To work around that, you need to peel away the leading ten characters (which I assume are static):

#!/bin/sh

cat "$@" |while read line; do
  echo "${line#??????????}"
done

As a one-liner, in bash, this can be simplified by using substrings:

while read L < filelist; do echo "${L:10}"; done

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

print only file names with along directory path

awk approach:

awk '{ sub(/^[[:space:]]*[^[:space:]][[:space:]]+/,"",$0) }1' filelist

The output:

src/layouts/PersonAccount-Person Account Layout.layout
src/objects/Case Account-Record List.object

----------

To extract only basename of the file:

awk -F'/' '{print $NF}' filelist

The output:

PersonAccount-Person Account Layout.layout
Case Account-Record List.object

Upvotes: 1

AGBhat
AGBhat

Reputation: 56

Try this out:

awk -F" " '{$1=""; print $0}' filelist | sed 's/^ //c'

Here sed is used to remove the first space of the output line.

Upvotes: 2

abhishek phukan
abhishek phukan

Reputation: 791

Can you try this once:

bash-4.4$ cat filelist |awk '{$1="";print $0}'
 src/layouts/PersonAccount-Person Account Layout.layout
 src/objects/Case Account-Record List.object   

else if you want to remove 2 columns it would be:

awk '{$1=$2="";print $0}'

This will produce the below output:

bash-4.4$ cat filelist |awk '{$1=$2="";print $0}'                                                                                                                                   
  Account Layout.layout                                                                                                                                                             
  Account-Record List.object   

Upvotes: 4

glenn jackman
glenn jackman

Reputation: 246807

A simple grep

grep -o '[^[:blank:]]*/.*' filelist

That's zero or more non-blank characters followed by a slash followed by the rest of the string.

This will not match any lines that don't have a slash

Upvotes: 1

Matias Barrios
Matias Barrios

Reputation: 5056

This will do exactly what you want for your example :

 sed -E 's/(.*)([ ][a-zA-Z0-9]+\/[a-zA-Z0-9]+\/[a-zA-Z0-9. -]+)/\2/g' filelist 

Explanation :

Its matching your path (including spaces if there were any ) and then replacing the whole line with that one match. Easy peasy lemon squeezy :)

Regards!

Upvotes: 1

Related Questions