Tag Archives: Linux - Page 9

From one row xml to nice print xml in a jiffy with xmllint

When I get to work with xml they are usually all on one row. This makes it impossible (or at least very hard) to read them. One solution I found was the superb –format option. This option will make the xmllint program to output the one row xml to a multiple nice print xml document.

Example
Our ugly one row xml (ugly.xml):

<?xml version="1.0" encoding="utf-8"?><movies><movie><title>The Brave One</title><genre>Thriller</genre></movie><movie><title>Instinct</title><genre>Drama</genre></movie></movies>

Run the command:

xmllint --format ugly.xml

or(if no file):

echo '<?xml version="1.0" encoding="utf-8"?><movies><movie><title>The Brave One</title><genre>Thriller</genre></movie><movie><title>Instinct</title><genre>Drama</genre></movie></movies>' | xmllint --format -

And you get:

<?xml version="1.0" encoding="utf-8"?>
<movies>
  <movie>
    <title>The Brave One</title>
    <genre>Thriller</genre>
  </movie>
  <movie>
    <title>Instinct</title>
    <genre>Drama</genre>
  </movie>
</movies>

Much better!

Pipe result into Vim

xmllint --format ugly.xml | vim -

Note the last ‘-‘ sign as it tells vim to read input from standard input.

Find text in files. A bunch of grep examples

Here is another command that I keep forgetting the syntax for so I decided to make a page with examples to use.

Find text in a file. Displays the row where the text ‘root’ is found in file /etc/group

grep root /etc/group

Find text in a file. Display row and row number in file

grep -n root /etc/group

Find text in many files. Display filename and rows

grep -r root /etc/* #You can combine 'r' and 'n' with -rn (or -nr)

Find rows in a file that starts with a specific text. Displays all rows in file /etc/group where the row starts with the text ‘root’

grep  ^root /etc/group

Find rows in a file that ends with a specific text. Displays all rows in file /etc/group where the row ends with the text ‘0:’

grep 0:$ /etc/group

Find a word in a row. A word is text that has a space or non-alpha characters in the beginning and the end. Displays all rows that contains the word ‘root’ in the file /etc/group

grep -w root /etc/group

Return rows before a found row.This will return all rows that contains the word ‘root’ AND the 2 closest rows above that row

grep -B2 root /etc/group

Return rows after a found row.This will return all rows that contains the word ‘root’ AND the 2 closest rows below that row

grep -A2 root /etc/group

NOTE: A and B can be combined to fetch more rows both below and above the matched row

Find rows that contains any of the specified characters. Display all rows that have one or more of the characters ‘0’,’1′,’2′,’a’,’b’,’c’ in file /ect/group

grep  [0-2a-c] /etc/group # or [012abc]. Note case-sensitive!

Find all rows with exact number of wildcards. This will find all rows with text that starts with ‘r’ and ends with ‘t’ AND have exactly 2 wildcard characters in the middle. Display rows

grep '<r..t>' /etc/group

Note escaping of the < and > characters

Find all rows with wildcards. This will find all rows with text that start with ‘r’ and ends with ‘t’ AND have zero or more characters in between.

grep '<r.*t>' /etc/group

SCP Examples

I’m always forgetting what parameters to use and in what order I should use them. Here is some examples to help me find what I’m looking for.

Syntax:
scp options from to

Copy one file from a computer to another with standard settings. The file will end up in my home directory on computerB:

scp filename user@computerB.com:.

Copy one file from computerA to computerB with port 222. The file will end up in /tmp on the computerB

scp -P 222 filename user@computerB.com:/tmp

Copy a folder from computerA to computerB. The folder and all its content will end up in my home directory in a folder called Backup

scp -r foldername user@computerB.com:Backup/

Copy one file from computerB to computerA. The file is in my home directory on computerB. The file will end up in my current directory on computerA

scp user@computerB.com:filename .

Copy a folder from computerB to computerA. The folder is found here /tmp/Backup on computerB and will end up in my home directory in a folder called tmp.

scp -r user@computerB.com:/tmp/Backup /home/user/tmp/