当前位置: 动力学知识库 > 问答 > 编程问答 >

shell - Bash for truncation

问题描述:

I have to make changes to a document where there are two columns separated by tab (\t) and each record separated by newline \n. the statements of the document are as follows:

 /something/random/2345.txt

my aim is to remove the entire string and just keep the number 2345 in this case.I used

 sed 's/something/random//g' file.csv

but I do not know how to escape the / cause sed syntax has / too. Also not all records have the same words so i would be looking for regex of the type

 /*/*.*

But each entry has a number as a part of the record and I would like to extract that.

Also there are a few records which do not contain any number, I would like to delete those records along with the corresponding entry in the next column for that record.

The file is in CSV format.

网友答案:

You can escape the forward slash with a backslash, or you can use a different character than forward slash to delimit your expression. Observe:

echo foobar | sed sIfooIcrowI
> crowbar

Of course, you probably shouldn't use an alphabetic character for the delimiter. I'm just using it here to make the point that pretty much any normal character can be substituted for the slash.

网友答案:

You could just remove all non digit characters from brining of each statement in string :

   sed 's/[^0-9]*\(.*\)[\t]*/\1/g'
分享给朋友:
您可能感兴趣的文章:
随机阅读: