Skip to main content

regexp inverse selection

selecting all rows not matchng (viasual studio code tested)

class="ms-Checkbox-text text-284">01/10/2020</span>

^((?!.*text-284.*\<\/span\>).)*$

To top

regexp no-greedy / lazy (stops on first occurence)

You need to make your regular expression non-greedy, because by default, "(.*)" will match all of "file path/level1/level2" xxx some="xxx".

Instead you can make your dot-star non-greedy, which will make it match as few characters as possible:

/location="(.*?)"/

Adding a ? on a quantifier (?, * or +) makes it non-greedy.

To top

Finding files in tar archives / extract selected file

tar -ft large_file.tar.gz | grep "the-filename-you-want"

Output:

"full-path/to-the-file/in-the-archive/the-filename-you-want"

tar -xf large_file.tar.gz "full-path/to-the-file/in-the-archive/the-filename-you-want"

To top

SED find and reuse

se in un file HTML

<p class="bodytext">mia Amica</p>
</td>
<td><p class="bodytext"><a href="javascript:linkTo_UnCryptMailto('nbjmrrrrrfhsfufsjb/dijnjdbcbtfAdijnjdbebhptujop/ju');">segreti@miaAmica.it</a>
</p>
</td>

 voglio che questo:

<a href="javascript:linkTo_UnCryptMailto('nbjmddddsfufsjb/dijnjdbcbtfAdijnjdbebhptujop/ju');">segreti@miaAmica.it</a>

diventi:

<a href="mailto:segreti@miaAmica.it">segreti@miaAmica.it</a>

sed 's/\(javascript:linkTo_UnCryptMailto(.*;\).*>\(.*\)<.*/mailto:\2">\2<\/a>/g'

To top

bash rename files removing first part of filename

stackoverflow.com/questions/28305134/remove-first-n-character-from-bunch-of-file-names-with-cut

 

01 Whistle.m4a


01 Wings.m4a
01 Without You.m4a
01 Wrecking Ball.m4a
01 Yeah!.m4a
01 You And I _ You And I.m4a
01 You Are Woman, I Am Man [feat. Io.m4a
01 You Are the Sunshine of My Life.m4a

for pippo in 01* ; do mv "$pippo" "`echo $pippo | cut -c 3-`"; done

 

mv 01 You're All I Need To Get By.m4a  You're All I Need To Get By.m4a
mv 01 You're All the World To Me.m4a  You're All the World To Me.m4a
mv 01 You're My Best Friend.m4a  You're My Best Friend.m4a
mv 01 You're The Top.m4a  You're The Top.m4a
mv 01 You've Lost That Lovin' Feelin'.m4a  You've Lost That Lovin' Feelin'.m4a
mv 01 Your Song.m4a  Your Song.m4a

 

To top

bash join pairs of consecutives lines

stackoverflow.com/questions/8545538/how-do-i-join-pairs-of-consecutive-lines-in-a-large-file-1-million-lines-using

with TAB as separators

$

seq 10 |paste - -

1    2
3    4
5    6
7    8
9    10

with space as separators

$ seq 10 |paste -d" " - -

1 2
3 4
5 6
7 8
9 10

with , as separators

$ seq 10 |paste -d", " - -

1,2
3,4
5,6
7,8
9,10

To top

search (and move) files created in a specific year

#!/bin/bash
OLDIFS=$IFS
IFS='
'
DESTDIR="parts/$1"
mkdir ${DESTDIR}
touch -d "01 january $1 00:00:00" startDate
touch -d "31 december $1 23:59:59" endDate
for FILENAME in `find parts/ -maxdepth 1 -newer ./startDate -and -not -newer ./endDate`
do
 mv -v ${FILENAME} ${DESTDIR}
done

IFS=$OLDIFS

To top

sed invert date format from DD-MM-YYYY to YYYY-MM-DD

file dates:

10-02-1984
02-04-1968
09-01-1977
01-09-1975
25-09-1966
08-12-1963
19-04-1990
28-01-1959
18-04-1957
03-11-1984
11-10-1965
22-02-1961
31-08-1962
18-04-1959
22-09-1959
30-06-1987
04-03-1981
30-05-1963
26-06-1984
01-01-1961
05-01-1976
20-02-1971
08-02-1968
16-02-1985

sed 's/\([0-9][0-9]\)\-\([0-9][0-9]\)\-\([0-9][0-9][0-9][0-9]\)/\3\-\2\-\1/g' dates > datesNew

file datesNew:

1984-02-10
1968-04-02
1977-01-09
1975-09-01
1966-09-25
1963-12-08
1990-04-19
1959-01-28
1957-04-18
1984-11-03
1965-10-11
1961-02-22
1962-08-31
1959-04-18
1959-09-22
1987-06-30
1981-03-04
1963-05-30
1984-06-26
1961-01-01
1976-01-05
1971-02-20
1968-02-08
1985-02-16

 

To top

sed invert date format from DD/MM/YYYY to YYYY-MM-DD

file dates:

29/11/2022
30/11/2022
01/12/2022
02/12/2022
05/12/2022
06/12/2022

([0-9]{2})/([0-9]{2})/([0-9]{4})

$3-$2-$1

file datesNew:

2022-11-29
2022-11-30
2022-12-01
2022-12-02
2022-12-05
2022-12-06

To top

bash: env variable reset when execute sudo cmd

when execute commands with sudo a new bash envoirment is loaded:

if you try:

sudo env |grep -i proxy

you will get an empty line.

adding this file

/etc/sudoers.d/proxy 

containing :

Defaults   env_reset
Defaults   env_keep += "HTTP_PROXY"
Defaults   env_keep += "HTTPS_PROXY"
Defaults   env_keep += "http_proxy"
Defaults   env_keep += "https_proxy"

now with:

sudo env |grep -i proxy

you'll have this output:

http_proxy=http://192.168.2.3:8080/
https_proxy=http://192.168.2.3:8080/
HTTPS_PROXY=http://192.168.2.3:8080/
HTTP_PROXY=http://192.168.2.3:8080/

so the env variables have passed to the sudo env 

 

To top

grep con OR

 egrep -i '(text1)|(text2)' filename

 

To top

check Garageband HW platform

#!/bin/bash
OLDIFS=$IFS
IFS='
'
for garages in `find . -name "*.band"`
do
  echo $garages: `egrep -i '(ios)|(x86_64)' $garages/projectData |cut -d \> -f 2|cut -d \< -f 1`
done
IFS=$OLDIFS


To top

convert GarageBand from Ipad to mac and back

Ipad to mac

#!/bin/bash
OLDIFS=$IFS
IFS='
'
for DIR in `ls -1 *.band|grep :|cut -d \: -f1`
do echo ${DIR}
  cd  ${DIR}
      sed -i.bak 's/iPad3,1/x86_64/' projectData
      sed -i.bak 's/ios/macos/' projectData
      sed -i.bak 's/ios/macos/' Output/metadata.plist
      sed -i.bak 's/iPad3,1/x86_64/' Output/metadata.plist
  cd ..
done
IFS=$OLDIFS

mac to Ipad

#!/bin/bash
OLDIFS=$IFS
IFS='
'
for DIR in `ls -1 *.band|grep :|cut -d \: -f1`
do echo ${DIR}
  cd  ${DIR}
      sed -i.bak 's/x86_64/iPad3,1/' projectData
      sed -i.bak 's/macos/ios/' projectData
      sed -i.bak 's/macos/ios/' Output//metadata.plist
      sed -i.bak 's/x86_64/iPad3,1/' Output//metadata.plist
  cd ..
done
IFS=$OLDIFS



 

To top

Rename Mp3 file from id3 Tags

for pippo in `ls -1`; do  eyeD3 --rename="../al3ssia/%n - %t" $pippo; done

OR

#!/bin/bash
TITLE="`id3tool "$1" | grep '^Song Title:' | awk '{ for (i=3;i<=NF;i++) { printf $i; printf " " } }'`"
ARTIST="`id3tool "$1" | grep '^Artist:' | awk '{ for (i=2;i<=NF;i++) { printf $i; printf " " } }'`"
ALBUM="`id3tool "$1" | grep '^Album:' | awk '{ for (i=2;i<=NF;i++) { printf $i; printf " " } }'`"
YEAR="`id3tool "$1" | grep '^Year:' | awk '{ for (i=2;i<=NF;i++) { printf $i; printf " " } }'`"
TRACKNUM="`id3tool "$1" | grep '^Year:' | awk '{ print $2 }'`"

OR

#!/bin/bash
TITLE="`id3info "$1" | grep '^=== TIT2' | sed -e 's/.*: //g'`"
ARTIST="`id3info "$1" | grep '^=== TPE1' | sed -e 's/.*: //g'`"
ALBUM="`id3info "$1" | grep '^=== TALB' | sed -e 's/.*: //g'`"
YEAR="`id3info "$1" | grep '^=== TYER' | sed -e 's/.*: //g'`"
TRACKNUM="`id3info "$1" | grep '=== TRCK' | sed -e 's/.*: //g'`"

echo "$ARTIST $TITLE $ALBUM $YEAR $TRACKNUM"

mv $1 "$ARTIST - $ALBUM - $TRACKNUM - $TITLE.mp3"

To top

move files from dir taking dirname (name with spaces) (copia 1)

O=$IFS
IFS=$'\n'
for pippo in `ls -1 -d */|cut -d / -f 1`
do
   mv -v "$pippo"/Game.cdz "$pippo".cdz
   rm -rf "$pippo"/
done
IFS=$O

 

To top

Remove or replace newlines using sed,awk,tr - BASH

http://unstableme.blogspot.com/2008/05/remove-or-replace-newlines-using.html


$ cat week.txt
Su
Mo
Tu
We
Th
Fr
Sa



Output Required:


- a) Remove the newlines. i.e. required output:
SuMoTuWeThFrSa

- b) Replace the newlines with "|", i.e.
Su|Mo|Tu|We|Th|Fr|Sa




Remove/Replace newlines with sed


a)
$ sed -e :a -e '$!N;s/\n//;ta' week.txt
SuMoTuWeThFrSa

b)
$ sed -e :a -e '$!N;s/\n/|/;ta' week.txt
Su|Mo|Tu|We|Th|Fr|Sa



One more way of doing. But not suitable for files with large number of records, as you see the number of N's is just 1 less than number of lines in the file.


a)
$ sed 'N;N;N;N;N;N;s/\n//g' week.txt
SuMoTuWeThFrSa

b)
$ sed 'N;N;N;N;N;N;s/\n/|/g' week.txt
Su|Mo|Tu|We|Th|Fr|Sa



Remove/Replace newlines with awk


a)
$ awk '{printf "%s",$0} END {print ""}' week.txt
SuMoTuWeThFrSa

b)
$ awk '{printf "%s|",$0} END {print ""}' week.txt
Su|Mo|Tu|We|Th|Fr|Sa|

So we need to remove the last "|" in the above output.

$ awk '{printf "%s|",$0} END {print ""}' week.txt | awk '{sub(/\|$/,"");print}'
Su|Mo|Tu|We|Th|Fr|Sa



Remove/Replace newlines with tr


a)
$ tr -d '\n' < week.txt
SuMoTuWeThFrSa

b)
$ tr '\n' '|' < week.txt
Su|Mo|Tu|We|Th|Fr|Sa|

Similarly we need to remove the last "|" from the above output:
$ tr '\n' '|' < week.txt | sed 's/|$//'
Su|Mo|Tu|We|Th|Fr|Sa

One more:<dl class="avatar-comment-indent" id="comments-block"><dd class="comment-body" id="Blog1_cmt-2271710975389617176">


$ jot -c 10 A
A
B
C
D
E
F
G
H
I
J

$ jot -c 10 A | paste -sd,
A,B,C,D,E,F,G,H,I,J

Moreover,

$ jot -c 10 A | tr '\n' ',' | sed -e "s/[A-Z]*/'&'/g" -e "s/,''/\n/g"
'A','B','C','D','E','F','G','H','I','J'


$ jot -c 10 A | awk -v x="'" '{ s=s sprintf(x "%s" x ",", $0) } END { sub(",$", "", s); print(s) }'
'A','B','C','D','E','F','G','H','I','J'

</dd></dl>

To top