User Tools

Site Tools


filtering_commands

If you want to send us your comments, please do so. Thanks
More on comments


Filtering commands

Filtering is used to convert a text file to an other format and / or add or remove characters

cut

cut - remove sections from each line of files

6.txt can be made with (a tab between “the” and “cut”

echo "This ' is \ a : test / using > the      cut < command" > 6.txt
  cut -d "/" -f 2,3,4 < 6.txt > 7.txt
  cut -d ">" -f 2,3,4,5 < 5.txt > 6.txt
  cut -d ' ' -f 3,4,6 < 6.txt > 7.txt # -d contains a space
  cut -d '\t' -f 2 | less # This does not work, only a single character is allowed. Past a real tab instead
  cut -d '	' -f 2 | less # Now -d contains a real tab
  cut -d \ -f 2,3,4 < 6.txt > 7.txt 
  cut -d > -f 2,3,4 < 5.txt > 6.txt
  cut -d \/ -f 2,3,4 < 6.txt > 7.txt

Using --delimiter with a space, and other characters

  cut --delimiter=i --fields=2,4 < 6.txt > 7.txt
  cut --delimiter=: --fields=2 < 6.txt > 7.txt
  cut --delimiter=' ' --fields=2 < 6.txt > 7.txt # Quotes are needed for a space
  cut --delimiter=\  --fields=2 < 6.txt > 7.txt # Or escape the space. Note: Two spaces after the \ are needed!
  cut --delimiter='	' --fields=2 < 6.txt > 7.txt # Using a tab
  cut --delimiter=\ --fields=2 < 6.txt > 7.txt # Does not work, the quotes are needed for a \ 
  cut --delimiter='\' --fields=2 < 6.txt > 7.txt
  cut --delimiter="'" --fields=2 < 6.txt > 7.txt # Here the double quotes are needed for a '

tr

Missing operand

In a script using operands without quotes sometimes fails and sometings not. Why is not clear to us. You get an error message

somecommand | tr -s [:blank:] | someothercommand

Solution: Always put quotes around [:blank:]

somecommand | tr -s '[:blank:]' | someothercommand

tr - translate or delete characters

Remove a lot of characters

In Dokuwiki this can be used to find a row with one “|” too much in a table

Not all characters can be used in the same command. So several instances of tr are needed. Some characters need to be escaped with a \

cat /tmp/somefile.txt | tr -d [:alnum:] | tr -d [=,-_*-] | tr -d [$] | tr -d [\&\"] | tr -d [\(\)\!] | tr -d [:blank:] > /tmp/otherfile.txt

Examples

cat sometile.txt | tr '\n' \; | tr \* '\n'
cat somefile.txt | tr -d [:alnum:] | less # Remove all letters and digits
tr '-' '\n' < 3.txt > 4.txt
tr '-' '\n' < 3.txt | sort > 4.txt
tr '/' '\t' < 7.txt > 8.txt
tr '/' 'x' < 3.txt > 4.txt
tr '<' '\t' < 9.txt > 10.txt
tr '\t' ' ' < 10.txt > 11.txt
tr '\t' ' ' < 3.txt > 4.txt 
tr [:blank:] '\t' < 13.txt > 14.txt
tr [:blank:] < 4.txt > 5.txt
tr -d '\-somestring' < 4.txt > 5.txt
tr -s "#" < 3.txt > 4.txt 
tr -s # < 3.txt > 4.txt 
tr -s ' ' < 11.txt > 12.txt
tr -s [:blank:] < 4.txt > 5.txt
tr -s [:space:] < 2.txt > 3.txt 

Does not work

tr \t ' ' < 3.txt > 4.txt # not the tabs but the t's are replaced by spaces
tr -d '-somestring' < 4.txt > 5.txt # -s is seen as an option. The - needs to be escaped: \-somestring

Main subjects on this wiki: Linux, Debian, HTML, Microcontrollers, Privacy

RSS
Disclaimer
Privacy statement
Bugs statement
Cookies
Copyright © : 2014 - 2022 Webevaluation.nl and the authors
Changes reserved.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
filtering_commands.txt · Last modified: 10-12-2021 14:19 by wim