Allan
Allan

Reputation: 41

How to cut some text in bash

After cat my file, I got following output:

pm/or { distribution: 'Old disctribution ', version_1: '6.5 (New)', version_2: '2.36' }

I would like to cut from this file only for example distribution name: Old disctribution or for example only version_2: 2.36.

As output I would like to get: Old disctribution

Could someone help how can to do it?

Upvotes: 0

Views: 83

Answers (5)

David C. Rankin
David C. Rankin

Reputation: 84561

Bash parameter expansion is also available from within a script to handle extracting the desired text:

#!/bin/bash

temp=`cat dat/catfile.txt`                     # read file into temp
temp="${temp##*ion: \'}"                       # remove begin to 'ion: \''
echo "string '${temp%%[^a-z]\',*version*}'"    # remove end to first version

input:

$ cat dat/catfile.txt
pm/or { distribution: 'Old disctribution ', version_1: '6.5 (New)', version_2: '2.36' }

output:

$ cutcat.sh
string 'Old disctribution'

(spelling as in original)

Upvotes: 0

John B
John B

Reputation: 3646

You can get the value of the distribution key with awk like this:

awk -F"'" '{for(;i<=NF;i++) {if($i ~ /distribution: $/) print $(i+1)}}' file

Upvotes: 0

Fappie.
Fappie.

Reputation: 490

Try to cut it with ' as a delimiter Something like:

cut -d \' -f 2,6 myfile.txt > output.txt

I just try this and it works properly

Upvotes: 1

tripleee
tripleee

Reputation: 189407

Looks vaguely like you are trying to parse JSON. If your file format is almost, but not entirely JSON, perhaps your best bet is to reshape it into proper JSON, then use a proper JSON tool.

 sed 's%^pm/or %%;s%\([_A-Za-z0-9]*\):%"\1":%g' myfile.txt | jq '.distribution'

The jq tool is available from http://stedolan.github.io/jq/

Upvotes: 0

konsolebox
konsolebox

Reputation: 75488

awk -F\' '{ print $2 }' file

Or

awk -F\' '/pm/or {/ { print $2 }' file

Upvotes: 0

Related Questions