how to use "cut" or "awk" or "sed" to remove a string

logs:

[Sun Feb 12 10:36:20 2012] "/home/abc/public_html/index.php"
[Sun Feb 12 10:36:20 2012] "/home/abc/public_html/index.php"
[Sun Feb 12 10:36:20 2012] "/home/xyz/public_html/index.php"
[Sun Feb 12 10:36:21 2012] "/home/xyz/public_html/index.php"
[Sun Feb 12 10:36:21 2012] "/home/xyz/public_html/index.php"

how to use "cut" or "awk" or "sed" to get the following result:

[Sun Feb 12 10:36:20 2012] abc
[Sun Feb 12 10:36:20 2012] abc
[Sun Feb 12 10:36:20 2012] xyz
[Sun Feb 12 10:36:21 2012] xyz
[Sun Feb 12 10:36:21 2012] xyz

nawk -F/ '{print $1$3}' log|tr -d \"
1 Like

nice! it works.

cat log | cut -d/ -f1,3 | tr -d \"/

Thr better version of this might be replacing cat by input redirection < operator .

One command:

awk -F'[/"]*' '{print $1$3}' log

Hi

can you please explain the below part:

awk -F'[/"]*'

Thanks

---------- Post updated at 07:27 PM ---------- Previous update was at 07:05 PM ----------

Pure bash solution for interest:

pandeeswaran@ubuntu:~$ while read line; do line1=`echo $line|cut -d/ -f1,3|tr -d \"/`; echo $line1; done < log
[Sun Feb 12 10:36:20 2012] abc
[Sun Feb 12 10:36:20 2012] abc
[Sun Feb 12 10:36:20 2012] xyz
[Sun Feb 12 10:36:21 2012] xyz
[Sun Feb 12 10:36:21 2012] xyz

it can also be script as...>>

awk -F'["/]' '{print $1$4}' r

---------- Post updated at 11:38 AM ---------- Previous update was at 11:35 AM ----------

also be scripted as---->>

awk -F'["/]' '{print $1$4}' r

---------- Post updated at 11:40 AM ---------- Previous update was at 11:38 AM ----------

awk -F'["/]' '{print $1$4}' r

@pandeesh: The field separator can be a regular expression. So -F'[/"]*' uses multiple occurrences of the character / and/or the character " as field separator. The fields of the first line thus become:

$1=[Sun Feb 12 10:36:20 2012] 
$2=home
$3=abc
$4=public_html
$5=index.php
$6=<empty>

-----
shell solution:

while IFS='/"' read f1 x x f4 x x x
do 
  echo "$f1$f4"
done < infile
1 Like