I have a file like this
number: string(200)
issueDate: datetime
expiryDate: datetime
file_path: string(200)
filename: string(200)
description: text
I am using this in perl to get ouputlike
FIELDS="number: string(200) issueDate: datetime expiryDate: datetime file_path: string(200) filename: string(200) description: text"
This is done by this command
perl -plne '$_ = "FIELDS=\""."$_" if $. == 1; $\ = " ";$_ = "\""."$_" if eof' document.txt
Now i have the full file like this
[entity]
JOHN
[BUNDLE]
mybundle
[FIELDS]
number: string(200)
issueDate: datetime
expiryDate: datetime
file_path: string(200)
filename: string(200)
description: text
Now i want the ouput to terminal like this in separate lines
ENTITY = JOHN
BUNDLE = Mybundle
FIELDS="number: string(200) issueDate: datetime expiryDate: datetime file_path: string(200) filename: string(200) description: text"
basically the variables NAME like ENTITY, BUNDLE, it should get from the file
how can i do that
You can do it with awk like this:
Explanation:
If I fiind a line starting with [entity] I grab the following line and save as "e"
If I find a line starting with [BUNDLE], I grab the following line and save as "b"
If I find a line with a colon, I append it to "f" where I save the fields (with added spaces)
If I find a line starting with "description", I print out what I have found so far and clear the fields variable "f".
Here's a fairly readable perl-ish version of my awk answer:
output
-l
chomps newline on input and add's it when usingprint
-00
reads input in paragraphs (these are terminated by two or more newlines)y|\n\r[]| |d
replaces newlines with spaces, deletes\r[]
chars, and returns number of how many chars were altered$q
is assigned"
char only when more than 3 chars were replaced (used forFIELDS
entry)s|||
substitution takes first non spaces chars (entity,bundle,fields), and inerts= $q
after them