0

I have this code

 cat SomeFile | tr -cs '[:alnum:]' '\n' |tr -d 0-9 | tr '[:upper:]' '[:lower:]' > net.txt

I want to read a file and ignore those characters.The output would be one word per line except if its a website.

 \'#$%.,:;?!&*|()[]"<>=-

how i can do this by using only cat and tr ?

the output should look like this

other
branches
examples
for
developers
http//drupalorg/project/examples
what
is
this
this
set
of

Thanks

2 Answers 2

2

You can use a couple of translations:

tr "'"'\#$%.,:;?!&*|()[]"<>=-' ' ' <SomeFile | tr -s '[:space:]' "\n"

The first operation converts any of the characters you don't want into a space. The second operation converts all whitspace (including newlines) into a newline, squeezing runs of newlines to a single character.

1

For an input SomeFile:

Examples:for9 developers>http://example.org/examples?s=%20&<what>
is, this?

produce an output of:

examples
for
developers
http://example.org/examples?s=%20&
what
is
this

I suppose this could be done using only tr+shell:

for i in $(<SomeFile tr -cs ']a-zA-Z0-9/:.%?=&_,+()~['\''#$;!*-' '\n' | \
    tr '[:upper:]' '[:lower:]'); do
    case "$i" in
        *://*)
            echo "$i" >> net.txt ;;
        *)
            for split in $(echo "$i" | tr -c 'a-z' '\n'); do
                echo "$split" >> net.txt
            done ;;
    esac
done

But it may be simpler to add grep to tr:

< SomeFile tr -cs ']a-zA-Z0-9/:.%?=&_,+()~['\''#$;!*-' '\n' | \
    tr '[:upper:]' '[:lower:]' | grep -o '.*://.*\|[a-z]*' > net.txt
  • There's no need for cat either – just direct the file into the standard input for tr

grep:

grep -oE '[a-zA-Z]+://[]a-zA-Z0-9/:.%?=&_,+()~['\''#$;!*-]+|[[:alpha:]]+' \
    -- SomeFile | tr '[:upper:]' '[:lower:]' > net.txt

With zsh an array could be used:

file=( ${(L)=$(< SomeFile)//[^]a-zA-Z0-9\/:.%?=&_,+()~[\'#$;!*-]/ } )
printf '%s\n' ${(M)file:#*://*} ${=${file:#*://*}//[^a-z]/ }
  • This prints all urls first, followed by any "words"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .