Score:1

How can I remove a string from every line of a file in the second column of a file

bt flag

The file structure is like this:

34945,/data/file/system01/data/system/backups/filename.txt
393092,/data/file/system-03stby/data/system/backups/filename.extra.txt

The result that I am looking for is this:

34945,/data/system/backups/filename.txt
393092,/data/system/backups/filename.extra.txt
hr flag
What rule should be used to remove the string? Should it be some kind of pattern match, or a specific number or directory components?
donna_unsworthcomcastnet avatar
bt flag
Will always be the first 3 directories in the path, i.e /data/file/system01 and the first 2 directories will be the same, the third one will be different
Score:2
hr flag

Given

$ cat file
34945,/data/file/system01/data/system/backups/filename.txt
393092,/data/file/system-03stby/data/system/backups/filename.extra.txt

then

$ sed -E 's:,(/[^/]*){3}:,:' file
34945,/data/system/backups/filename.txt
393092,/data/system/backups/filename.extra.txt

or similarly

$ awk -F, 'BEGIN{OFS=FS} {sub(/([/][^/]*){3}/,"",$2)} 1' file
34945,/data/system/backups/filename.txt
393092,/data/system/backups/filename.extra.txt

In either case, ([/][^/]*){3} is an extended regular expression matching a slash, followed by zero or more non-slash characters, all 3 times. If the first two directories are constant, you could use something like sed 's:,/data/file/[^/]*:,:' or awk -F, 'BEGIN{OFS=FS} {sub(/data[/]file[/][^/]*[/]/,"",$2)} 1'.

Or splits and joins in perl:

$ perl -F, -lpe '@p = split /\//, $F[1]; $_ = join ",", $F[0], (join "/", @p[4..$#p])' file
34945,data/system/backups/filename.txt
393092,data/system/backups/filename.extra.txt
terdon avatar
cn flag
I think you could just use `sed -E 's:(/[^/]*){3}::' file`. Non need for the comma.
hr flag
@terdon yeah I really only threw that in to unambiguously anchor it to the start of the second field
terdon avatar
cn flag
Hmm yes, and that's probably a good idea. The OP did mention the "second column".
Score:1
cn flag

Another Perl way, same basic idea as @steeldriver's answer, but more concise:

$ perl -F'[,/]' -nle 'print "$F[0],/",join("/",@F[5..$#F])' file
34945,/data/system/backups/filename.txt
393092,/data/system/backups/filename.extra.txt
Score:-1
it flag

Read man cut and use cut "-d," -f2- | cut -d/ -f5- on your file.

terdon avatar
cn flag
That would result in `data/system/backups/filename.txt` and not `34945,/data/file/system01/data/system/backups/filename.txt` as requested. Also, `,` are not special, there's no need to quote the `"-d,"`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.