del
del

Reputation: 199

bash script to strip out some characters

Bash scripting. How can i get a simple while loop to go through a file with below content and strip out all character from T (including T) using sed

"2012-05-04T10:16:04Z"
"2012-04-05T15:27:40Z"
"2012-03-05T14:58:27Z"
"2011-11-29T15:04:09Z"
"2011-11-16T12:12:00Z"

Thanks

Upvotes: 2

Views: 392

Answers (3)

Cyrus
Cyrus

Reputation: 88626

With bash builtins:

while IFS='T"' read -r a a b; do echo "$a"; done < filename

Output:

2012-05-04
2012-04-05
2012-03-05
2011-11-29
2011-11-16

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174706

Through sed,

sed 's/"\|T.*//g' file

"matches double quotes \| or T.* starts from the first T match all the characters upto the last. Replacing the matched characters with an empty string will give you the desired output.

Example:

$ echo '"2012-05-04T10:16:04Z"' | sed 's/"\|T.*//g'
2012-05-04

Upvotes: 2

anubhava
anubhava

Reputation: 785146

A simple awk command to do this:

awk -F '["T]' '{print $2}' file
2012-05-04
2012-04-05
2012-03-05
2011-11-29
2011-11-16

Upvotes: 2

Related Questions