简体   繁体   中英

grep for a pattern in each line

I have a large file with the below URLs and want to create different directories for each URL in a bash script giving the below file named URLs as input to the bash script.

www.example.com/B000742278U3/B000742278U3.m3u8?p=33&e=1481028335&h=5380934bbca73e3e6de52299b92368b1
www.example.com/B000742279U3/B000742279U3.m3u8?p=33&e=1481028335&h=5380934bbca73e3e6de52299b92368b1
www.example.com/B000742280U3/B000742280U3.m3u8?p=33&e=1481028335&h=5380934bbca73e3e6de52299b92368b1

Expected output:

B000742278U3
B000742279U3
B000742280U3

I tried the command below, but couldn't achieve what I wanted:

echo $1
mkdir $(echo $1 | sed 's/.*B\(.*\)3/\1/')
cd $(echo $1 | sed 's/.*B\(.*\)3/\1/')

You can just use the slash as a field delimiter in cut :

$ cut -d '/' -f 2 infile
B000742278U3
B000742279U3
B000742280U3

你可以试试这个

 cut -d '/' -f2 | xargs mkdir

这是awk版本:

awk -F/ '{print $2}' | xargs mkdir

awk -F'[./]' '{print $5}' file

B000742278U3
B000742279U3
B000742280U3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM