Lark
Lark

Reputation: 620

Match "/" Once In Awk

I'm trying to use awk to pull out particular substring in a file path. Given that I have something coming into the awk command as a/workspace/folder.fold/secondfolder/file.ext, how do I get it to result as folder.fold/secondfolder/file.ext?

I was trying to do

| awk -F "/" '{ print $2 }'

but that only gives me folder. Any help is certain appreciated, as I'm very new to awk.

Edit: It's actually coming in as a/workspace/folder.fold/secondfolder/file.ext. The extra a/ gets pulled as $1 in my attempts.

Upvotes: 2

Views: 156

Answers (3)

Mirage
Mirage

Reputation: 31548

you can try sed

sed -re 's@([^/+]/)([^/]+/)@@' temp.txt

Input

a/workspace/folder.fold/secondfolder/file.ext

Output

folder.fold/secondfolder/file.ext

Upvotes: 1

SHAILESH PATEL
SHAILESH PATEL

Reputation: 71

some of the ways to achieve desired output are

echo '/workspace/folder.fold/secondfolder/file.ext' | awk -F/ '{for(i=3;i<=NF;i++) printf("%s" ,$i FS)}

echo '/workspace/folder.fold/secondfolder/file.ext' | cut -d / -f 3-

Upvotes: 1

William Pursell
William Pursell

Reputation: 212268

awk is not the best tool for this, but you can do:

awk '{ sub( "/[^/]*/","")}1'

Upvotes: 2

Related Questions