|
From: Giacomo on 27 Oct 2005 05:49 Janis Papanagnou wrote: > What type of shell or programs you do have to use? GNU bash, version 3.00.16(1)-release (i386-pc-linux-gnu) > What have you tried to program thus far? I tried "expr", but I think it can't work. Giacomo.
From: Stephane Chazelas on 27 Oct 2005 06:23 On Thu, 27 Oct 2005 01:09:37 +0200, Giacomo wrote: > I need to extract a substring of n adjacent digits from every single > line of a file. The position of the n digits are different from line to > line. > > For example: > > asdasd 123 asd 191991 1234 > lijoioi 4567 asdi 67567 iojoii > > For n=4 the result for each line must be 1234 e 4567. [...] n=4 sed -n "s/.*/+&+/ s/.*[^0-9]\([0-9]\{$n\}\)[^0-9].*/\1/p" < file That returns the right-most sequence of 4 digits. -- Stephane
From: Dave Gibson on 27 Oct 2005 16:05 Giacomo <a(a)b.cde> wrote: > I need to extract a substring of n adjacent digits from every single > line of a file. The position of the n digits are different from line to > line. > > For example: > > asdasd 123 asd 191991 1234 > lijoioi 4567 asdi 67567 iojoii > > For n=4 the result for each line must be 1234 e 4567. > > Thanks in advance, > Giacomo. #! /usr/bin/awk -f BEGIN { sequence = "0123456789" reqlen = count > 0 ? count : 4 for (i = 1; i <= reqlen; i++) pat = pat "[0-9]" } $0 ~ pat { for (i = 1; i <= NF; i++) { s = $i while (s ~ pat) { p = substr(s, 1, reqlen) if (index(sequence, p)) { # printf "%s, line %d (field %d): %s\n", FILENAME, FNR, i, $i print p next } s = substr(s, 2) } } } Copy that into a file and use it like this (replace "script.awk" with whatever you name it): awk -v count=4 -f script.awk your_data.files If you don't ask for a specific sequence length (with -v count=N) the script assumes 4.
From: Dan Mercer on 27 Oct 2005 19:17 "William Park" <opengeometry(a)yahoo.ca> wrote in message news:f2abd$43603ea4$d8fe9d17$6594(a)PRIMUS.CA... : Giacomo <a(a)b.cde> wrote: : > I need to extract a substring of n adjacent digits from every single : > line of a file. The position of the n digits are different from line to : > line. : > : > For example: : > : > asdasd 123 asd 191991 1234 : > lijoioi 4567 asdi 67567 iojoii : > : > For n=4 the result for each line must be 1234 e 4567. : : a='asdasd 123 asd 191991 1234 lijoioi 4567 asdi 67567 iojoii' : RE='\<[0-9]{4}\>' : echo "${a|+$RE}" This can be down in the shell. Assuming lines containing only lower case letters and numbers: ifs=$IFS IFS="${IFS}abcdefghijklmnopqrstuvwxyz" while read line do IFS=$ifs set -- $line IFS=$ifs set -- $* for i do ((${#i} == 4)) && echo "$i" done done Now, this isn't the efficient way to do it. That would probably be best done in perl. Dan Mercer : : Ref: : http://home.eol.ca/~parkw/index.html#parameter_expansion : : -- : William Park <opengeometry(a)yahoo.ca>, Toronto, Canada : ThinFlash: Linux thin-client on USB key (flash) drive : http://home.eol.ca/~parkw/thinflash.html : BashDiff: Super Bash shell : http://freshmeat.net/projects/bashdiff/
From: William Park on 27 Oct 2005 21:25 Dan Mercer <dmercer(a)mn.rr.com> wrote: > "William Park" <opengeometry(a)yahoo.ca> wrote in message news:f2abd$43603ea4$d8fe9d17$6594(a)PRIMUS.CA... > : Giacomo <a(a)b.cde> wrote: > : > asdasd 123 asd 191991 1234 > : > lijoioi 4567 asdi 67567 iojoii > : > > : > For n=4 the result for each line must be 1234 e 4567. > : > : a='asdasd 123 asd 191991 1234 lijoioi 4567 asdi 67567 iojoii' > : RE='\<[0-9]{4}\>' > : echo "${a|+$RE}" > > This can be down in the shell. Assuming lines containing only lower case letters and > numbers: > > ifs=$IFS > IFS="${IFS}abcdefghijklmnopqrstuvwxyz" > while read line > do > IFS=$ifs > set -- $line > IFS=$ifs > set -- $* > for i > do > ((${#i} == 4)) && echo "$i" > done > done > > Now, this isn't the efficient way to do it. That would probably be > best done in perl. Interesting approach. I would probably do it as for i in `tr -c '0-9' ' ' < file`; do [ ${#i} -eq 4 ] && echo $i done or if there is lots of repetition, func() { [ ${#1} -eq 4 ] } set -- `tr -c '0-9' ' ' < file` echo ${@|?func} -- William Park <opengeometry(a)yahoo.ca>, Toronto, Canada ThinFlash: Linux thin-client on USB key (flash) drive http://home.eol.ca/~parkw/thinflash.html BashDiff: Super Bash shell http://freshmeat.net/projects/bashdiff/
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: how to get yesterdays file ? Next: Modifying a STRING in C-Shell |