Ignore specific patterns in line using sed - unix

I'm trying to grep the word which starts with group keyword & ends with -wx in the given line. Also I need to ignore the below words.
Starts with default:group and ends with -wx
group::-wx
My Findings
echo "# file: /test/test123 # owner: own # group: acct user::r-- group::r-x mask::rwx other::r-x default:user::r-- default:user:an:--x default:group::r-x default:group:fin:-wx default:mask::rwx default:other::r-x" | grep -o "group:[^ ]*-wx" | sed '/group::-wx/d';'/default:[^ ]*:[^ ]*-wx/d'
Actual result
fin:-wx
Expected result
<null>

You already have a grep to select what you want, simply add grep statements to remove those you do not want.
Like so:
LINE="# file: /test/test123 # owner: own # group: acct user::r-- group::r-x mask::rwx other::r-x default:user::r-- default:user:an:--x default:group::r-x default:group:fin:-wx default:mask::rwx default:other::r-x"
echo $LINE | grep -o "group:[^ ]*-wx" \
| grep -vo "default:group:[^ ]*-wx" \
| grep -vo "group::-wx"
On my linux it returns nothing, which is what you expected. I do not have other test samples, but I think this is ok.

As you are first extracting the substring group:fin:-wx out of
default:group:fin:-wx with grep, the following sed filter
/default:[^ ]*:[^ ]*-wx/d no longer works.
A workaround is to change the order of filtering:
str="# file: /test/test123 # owner: own # group: acct user::r-- group::r-x mask::rwx other::r-x default:user::r-- default:user:an:--x default:group::r-x default:group:fin:-wx default:mask::rwx default:other::r-x"
echo "$str" | sed -e 's/default:group:[^ ]*-wx//' -e 's/group::-wx//' | grep -o 'group:[^ ]*-wx'
As an alternative, if your grep supports -P option, you can make use of positive lookbehind as:
echo "$str" | grep -Po '(?<= )group:[^ ]*-wx' | sed -e '/group::-wx/d' -e '/default:[^ ]*:[^ ]*-wx/d'
The pattern (?<= ) forces the pattern match preceded by a whitespace without
including it in the output.

Related

Not able to read file content with sed command

I am trying to read the below file line by line to perform the below operations
Extract the name of the file/directory alone and assign it one variable,
Extract the permission available in the line and add comma between the permission. Then assign it to another variable,
At last applying setfacl logic as shown in the output section.
File
# file: /disk1/script_1/ user::rwx group::r-x group:service:r-x mask::r-x other::r-x
# file: /disk1/script_1//hello.txt user::rw- group::r-- other::r--
# file: /disk1/script_1//bkp_10.txt user::rwx group::r-x other::r-x
Code
input="bkp_23.txt"
while IFS= read -r line;
do
echo $line
file_name=`sed -e 's/# file:\(.*\)/\1/g' "$line" | awk '{print $1}'`
echo $file_name
file_perm=`sed -e 's/# file:\(.*\)/\1/g' "$line" | awk '{$1=""}{print}' | tr ' ' ',' | awk
'{sub(",","")}1'`
echo $file_perm
echo "setfacl -m "$file_perm" "$file_name" executing"
done <"$input"
Output
setfacl -m user::rwx,group::r-x,group:service:r-x,mask::r-x,other::r-x /disk1/script_1/
setfacl -m user::rw-,group::r--,other::r-- /disk1/script_1//hello.txt
setfacl -m user::rwx,group::r-x,other::r-x /disk1/script_1//bkp_10.txt
Error
sed: can't read # file: /disk1/script_1/ user::rwx group::r-x group:service:r-x mask::r-x other::r-x: No such file or directory
$ cat input
# file: /disk1/script_1/ user::rwx group::r-x group:service:r-x mask::r-x other::r-x
# file: /disk1/script_1//hello.txt user::rw- group::r-- other::r--
# file: /disk1/script_1//bkp_10.txt user::rwx group::r-x other::r-x
$ while read _ _ path perms; do perms="$(echo "$perms" | tr -s ' ' ,)"; echo path="$path", perms="$perms"; done < input
path=/disk1/script_1/, perms=user::rwx,group::r-x,group:service:r-x,mask::r-x,other::r-x
path=/disk1/script_1//hello.txt, perms=user::rw-,group::r--,other::r--
path=/disk1/script_1//bkp_10.txt, perms=user::rwx,group::r-x,other::r-x
Try to echo the line content along with sed logic like this
file_name=$(echo "$line" | sed 's/# file:\(.*\)/\1/g' | awk '{print $1}')
file_perm=$(echo "$line" | sed -e 's/# file:\(.*\)/\1/g' | awk '{$1=""}{print}' | tr ' ' ',' | awk '{sub(",","")}1')

Using sed to enclose matches in double quotes

I'm trying to extract headers from emails and create a JSON fragment from them. I'm using sed to pull out the keys and values, but it's failing to put the trailing quote on each of the lines:
$ cat email1 | grep -i -e "^subject:" -e "^from:" -e "^to:" | \
sed -n 's/\^([^:]*\):[ ]*\(.*\)$/"\1":"\2"/gp'
"From":"Blah Blech <blah.blech#blahblech.com>
"To":"foo#bar.com
"Subject":"Yeah
I don't understand why the replacement pattern isn't working.
awk to the rescue!
$ awk -F": *" -vOFS=":" -vq="\"" 'tolower($0)~/^from|to|subject/
{print q$1q,q$2q}' email1
which combines cat or grep steps as well.
Stripping the carriage returns as #tripleee suggested fixed the issue with sed (using ctrl-v ctrl-m to capture the literal carriage return):
$ cat email1 | tr -d '^M' | grep -i -e "^subject:" -e "^from:" -e "^to:" | \
sed -n 's/^\([^:]*\):[ ]*\(.*\)$/"\1":"\2"/gp'
"From":"Blah Blech <blah.blech#blahblech.com>"
"To":"foo#bar.com"
"Subject":"Yeah"

Unix script to recursively search a directory and sub directories to grep and print content between 2 patterns in file

I have some files in a directory and sub directories. I need to search all the files and print the file name and the content between 2 matching patterns in the file.
For e.g. lets say my file looks like below.
File1.txt:
Pattern1
ABCDEFGHI
Pattern2
dafoaf
fafaf
dfadf
afadf
File2.txt
Pattern1
XXXXXXXXX
Pattern2
kdfaf
adfdaf
fdafad
I need to get following output
File1.txt:
ABCDEGHI
File2.txt:
XXXXXXXX
and so on for all the files under directory and sub directories separated by new line.
This might work for you:
find . \
-type f \
-exec awk 'BEGING {print FILENAME ":"} /Pattern1/ { p=1 ; next } /Pattern2/ {p=0} p==1 {print $0} END {print ""}' \{\} \;
Note, this prints the FILENAME, even if Pattern1 was not found!
This will work for you :
Create this shell script as my_grep.sh
#!/bin/sh
grep -nH "Pattern" $1 >>temp
if [ `grep -c $1 temp` -eq 2 ]; then
limits=`grep $1 temp | cut -f2 -d:`
lower_limit=`echo $limits | cut -f1 -d" "`
upper_limit=`echo $limits | cut -f2 -d" "`
echo "$1:"
head -`expr $upper_limit - 1` $1 | tail -`expr $upper_limit - $lower_limit - 1`
fi
Use find command to search files and fire this schell script:
$ find ./test -type f -exec ./my_grep {} \;
./test/File1.txt:
ABCDEFGHI
./test/File2.txt:
XXXXXXXXX

How do I Get the distinct List of Special Characters from a File using GREP or SED?

I have a file which contains about 30000 Records delimited by '|'. I need to get a distinct list of special characters only from the file.
For Eg:
123|fasdf|%df&|pap,came|!
234|%^&asdf|34|'":|
My output should be:
|%&,!^'":
Any help would be greatly appreciated.
Thanks,
Velraj.
grep -o '[|%&,!^":]' input | sort -u
You have to list all your special characters inside brackets.
This will return each unique special character on its own line. If you really need a string with these characters you have to remove newlines afterwards, e.g.:
grep -o '[|%&,!^":]' input | sort -u | tr -d '\n'
UPDATE:
If you need to remove all characters which are not from 'a-zA-Z0-9' set then you can use this one:
grep -o '[^a-zA-Z0-9]' input | sort -u | tr -d '\n'
echo "123|fasdf|%df&|pap,came|! 234|%^&asdf|34|'\":|" \
| { tr -d '[[:alnum:]]'; printf "\n"; } \
| sed 's/\(.\)/\1_/g' \
| awk -v 'RS=_' '{print $0}' \
| sort -u \
| awk '{printf $0}END{printf "\n"}'
output
!"%&',:^||
You can replace the first line echo .... with cat fileName

grep - put part of string into variable

I'm using swfdump to be able to get the ID number of an audio file. Here is what I'm using:
swfdump -D /Users/home/folder/file.swf | grep -i mp3
That is outputting:
[00e] 28999 DEFINESOUND defines id 0006 (MP3 22Khz 16Bit mono)
What I need is the id #..in this case it is 0006. i want that number in a variable. Anyone know how to do this?
the_id=`swfdump -D /Users/home/folder/file.swf | grep -i mp3 | cut -d' ' -f6`
var=$(swfdump -D /Users/home/folder/file.swf | sed '/MP3/s/.*id //;s/ (.*//')
or Bash
$ s=$(swfdump -D /Users/home/folder/file.swf)
$ var=${s/(MP3*}
$ echo $var
0006

Resources