Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk to remove characters from start of string in specific column

Tags:

awk

gsub

Text as follows:

1.6M    2014-08-21 20:56        ./file1
1.6M    2014-08-22 10:59        ./file2
24K     2014-08-26 10:39        ../file3
0       2014-08-21 14:12        ./file4
0       2014-08-22 18:05        ../file5
1.5M    2014-08-22 04:15        ./file6
8.0K    2014-08-20 20:31        ../file7

I want the output to be time ordered:

8.0K    2014-08-20 20:31        ../file7
0       2014-08-21 14:12        ./file4
1.6M    2014-08-21 20:56        ./file1
1.5M    2014-08-22 04:15        ./file6
1.6M    2014-08-22 10:59        ./file2
0       2014-08-22 18:05        ../file5
24K     2014-08-26 10:39        ../file3

Then I want the leading ./ to be removed but NOT ../ and only size and filename to be printed...

So far I've got:

sort -k 2 | awk 'BEGIN{FS="\t"; OFS="\t"} {gsub(/.\//, ""); print}'

Which gives:

8.0K        .file7
0           file4
1.6M        file1
1.5M        file6
1.6M        file2
0           .file5
24K         .file3

How can I make gsub only apply to start (first 2 characters) of coloumn 3 so that ../fileX doesn't become .fileX?

like image 537
Patrick Avatar asked Dec 04 '25 17:12

Patrick


2 Answers

I figured it out... was very close :), just needed to slightly alter gsub command

Edited

{gsub(/^\.\//, "", $3)
like image 65
Patrick Avatar answered Dec 06 '25 15:12

Patrick


Nice you figured a way out. Here's my solution with sed.

sort -k 2 tt | sed -r 's| \.{1}/| |'

Example:

sdlcb@Goofy-Gen:~/AMD$ sort -k 2 tt | sed -r 's| \.{1}/| |'
8.0K    2014-08-20 20:31        ../file7
0       2014-08-21 14:12        file4
1.6M    2014-08-21 20:56        file1
1.5M    2014-08-22 04:15        file6
1.6M    2014-08-22 10:59        file2
0       2014-08-22 18:05        ../file5
24K     2014-08-26 10:39        ../file3

Here the idea is to simply remove "./" which is preceded by a space. i.e remove " ./" or in other words, substitute " ./" with " " (space).substitution with space is only to maintain the indentation. -r in sed command is for regular expression support. sed 's|a|b|' means substitute first occurrence of "a" with "b". Thus in our case, substitution happens in 's| .{1}/| |. " .{1}/" means "single space followed by 1 '.' character". dot character needs to be escaped else sed interprets it as any character.

like image 39
Arjun Mathew Dan Avatar answered Dec 06 '25 17:12

Arjun Mathew Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!