Parse header in a file and based on the header replace a value in the file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
1












I have the following file



Field1|Field2|Field3|Field4|Field5
a|b|c|d|e
1|2|3|4|5
z|y|x|w|v


I have a script which accepts two inputs



script.sh Field3 T


The script.sh would take the 'Field3' argument and based on that column number should replace the contents with 'T'.



[edit]



I tried the following suggestion from @Costas and ran into a problem.
My file is like this:



MODULE_ColumnA~MODULE_ColumnB~MODULE_ColumnC~MODULE_ColumnD~MODULE_ColumnE
8.2~Y~N~~0


If I run the following script



cat <filename> | awk -F'~' -v p='MODULE_ColumnD' -v r='99' 'NR==1for (i=1;i<=NF;i++) if($i==p)f=i;print;next$f=r'


It replaces the value, but the delimiter is missing or replaced with space in the output as such:



MODULE_ColumnA~MODULE_ColumnB~MODULE_ColumnC~MODULE_ColumnD~MODULE_ColumnE
8.2 Y N 99 0


How do I get back the delimiter in the records?










share|improve this question























  • Why don't you pass 3 instead of Field3?
    – cuonglm
    Mar 5 '15 at 17:29










  • @cuonglm I just pasted an example to make it simpler to explain, in the actual file, the column headers are named differently and do not have the numeric suffix to them.
    – rahul
    Mar 5 '15 at 17:42






  • 1




    I mean if you pass the column number, it's easier awk -F'|' -v f=3 -v v=T 'FNR>1$f=T1' OFS='|' file
    – cuonglm
    Mar 5 '15 at 17:51










  • @cuonglm Thanks, yes it will make it easier. But the file has about 30 columns, and this script will be used as an utility for other users. So i wanted to make it simpler for them to use.
    – rahul
    Mar 5 '15 at 17:58














up vote
0
down vote

favorite
1












I have the following file



Field1|Field2|Field3|Field4|Field5
a|b|c|d|e
1|2|3|4|5
z|y|x|w|v


I have a script which accepts two inputs



script.sh Field3 T


The script.sh would take the 'Field3' argument and based on that column number should replace the contents with 'T'.



[edit]



I tried the following suggestion from @Costas and ran into a problem.
My file is like this:



MODULE_ColumnA~MODULE_ColumnB~MODULE_ColumnC~MODULE_ColumnD~MODULE_ColumnE
8.2~Y~N~~0


If I run the following script



cat <filename> | awk -F'~' -v p='MODULE_ColumnD' -v r='99' 'NR==1for (i=1;i<=NF;i++) if($i==p)f=i;print;next$f=r'


It replaces the value, but the delimiter is missing or replaced with space in the output as such:



MODULE_ColumnA~MODULE_ColumnB~MODULE_ColumnC~MODULE_ColumnD~MODULE_ColumnE
8.2 Y N 99 0


How do I get back the delimiter in the records?










share|improve this question























  • Why don't you pass 3 instead of Field3?
    – cuonglm
    Mar 5 '15 at 17:29










  • @cuonglm I just pasted an example to make it simpler to explain, in the actual file, the column headers are named differently and do not have the numeric suffix to them.
    – rahul
    Mar 5 '15 at 17:42






  • 1




    I mean if you pass the column number, it's easier awk -F'|' -v f=3 -v v=T 'FNR>1$f=T1' OFS='|' file
    – cuonglm
    Mar 5 '15 at 17:51










  • @cuonglm Thanks, yes it will make it easier. But the file has about 30 columns, and this script will be used as an utility for other users. So i wanted to make it simpler for them to use.
    – rahul
    Mar 5 '15 at 17:58












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have the following file



Field1|Field2|Field3|Field4|Field5
a|b|c|d|e
1|2|3|4|5
z|y|x|w|v


I have a script which accepts two inputs



script.sh Field3 T


The script.sh would take the 'Field3' argument and based on that column number should replace the contents with 'T'.



[edit]



I tried the following suggestion from @Costas and ran into a problem.
My file is like this:



MODULE_ColumnA~MODULE_ColumnB~MODULE_ColumnC~MODULE_ColumnD~MODULE_ColumnE
8.2~Y~N~~0


If I run the following script



cat <filename> | awk -F'~' -v p='MODULE_ColumnD' -v r='99' 'NR==1for (i=1;i<=NF;i++) if($i==p)f=i;print;next$f=r'


It replaces the value, but the delimiter is missing or replaced with space in the output as such:



MODULE_ColumnA~MODULE_ColumnB~MODULE_ColumnC~MODULE_ColumnD~MODULE_ColumnE
8.2 Y N 99 0


How do I get back the delimiter in the records?










share|improve this question















I have the following file



Field1|Field2|Field3|Field4|Field5
a|b|c|d|e
1|2|3|4|5
z|y|x|w|v


I have a script which accepts two inputs



script.sh Field3 T


The script.sh would take the 'Field3' argument and based on that column number should replace the contents with 'T'.



[edit]



I tried the following suggestion from @Costas and ran into a problem.
My file is like this:



MODULE_ColumnA~MODULE_ColumnB~MODULE_ColumnC~MODULE_ColumnD~MODULE_ColumnE
8.2~Y~N~~0


If I run the following script



cat <filename> | awk -F'~' -v p='MODULE_ColumnD' -v r='99' 'NR==1for (i=1;i<=NF;i++) if($i==p)f=i;print;next$f=r'


It replaces the value, but the delimiter is missing or replaced with space in the output as such:



MODULE_ColumnA~MODULE_ColumnB~MODULE_ColumnC~MODULE_ColumnD~MODULE_ColumnE
8.2 Y N 99 0


How do I get back the delimiter in the records?







bash shell-script sed awk perl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 20:35









Rui F Ribeiro

38.2k1475123




38.2k1475123










asked Mar 5 '15 at 17:11









rahul

1,025517




1,025517











  • Why don't you pass 3 instead of Field3?
    – cuonglm
    Mar 5 '15 at 17:29










  • @cuonglm I just pasted an example to make it simpler to explain, in the actual file, the column headers are named differently and do not have the numeric suffix to them.
    – rahul
    Mar 5 '15 at 17:42






  • 1




    I mean if you pass the column number, it's easier awk -F'|' -v f=3 -v v=T 'FNR>1$f=T1' OFS='|' file
    – cuonglm
    Mar 5 '15 at 17:51










  • @cuonglm Thanks, yes it will make it easier. But the file has about 30 columns, and this script will be used as an utility for other users. So i wanted to make it simpler for them to use.
    – rahul
    Mar 5 '15 at 17:58
















  • Why don't you pass 3 instead of Field3?
    – cuonglm
    Mar 5 '15 at 17:29










  • @cuonglm I just pasted an example to make it simpler to explain, in the actual file, the column headers are named differently and do not have the numeric suffix to them.
    – rahul
    Mar 5 '15 at 17:42






  • 1




    I mean if you pass the column number, it's easier awk -F'|' -v f=3 -v v=T 'FNR>1$f=T1' OFS='|' file
    – cuonglm
    Mar 5 '15 at 17:51










  • @cuonglm Thanks, yes it will make it easier. But the file has about 30 columns, and this script will be used as an utility for other users. So i wanted to make it simpler for them to use.
    – rahul
    Mar 5 '15 at 17:58















Why don't you pass 3 instead of Field3?
– cuonglm
Mar 5 '15 at 17:29




Why don't you pass 3 instead of Field3?
– cuonglm
Mar 5 '15 at 17:29












@cuonglm I just pasted an example to make it simpler to explain, in the actual file, the column headers are named differently and do not have the numeric suffix to them.
– rahul
Mar 5 '15 at 17:42




@cuonglm I just pasted an example to make it simpler to explain, in the actual file, the column headers are named differently and do not have the numeric suffix to them.
– rahul
Mar 5 '15 at 17:42




1




1




I mean if you pass the column number, it's easier awk -F'|' -v f=3 -v v=T 'FNR>1$f=T1' OFS='|' file
– cuonglm
Mar 5 '15 at 17:51




I mean if you pass the column number, it's easier awk -F'|' -v f=3 -v v=T 'FNR>1$f=T1' OFS='|' file
– cuonglm
Mar 5 '15 at 17:51












@cuonglm Thanks, yes it will make it easier. But the file has about 30 columns, and this script will be used as an utility for other users. So i wanted to make it simpler for them to use.
– rahul
Mar 5 '15 at 17:58




@cuonglm Thanks, yes it will make it easier. But the file has about 30 columns, and this script will be used as an utility for other users. So i wanted to make it simpler for them to use.
– rahul
Mar 5 '15 at 17:58










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










awk -F'|' -v p='Field3' -v r='T' '
NR==1
for (i=1;i<=NF;i++)
if ($i==p)
f=i
print
next
$f=r
'


If you like to format it to use like script.awk save it like:



#!/usr/bin/awk -f
BEGIN
OFS=FS="
NR==1
for (i=1;i<=NF;i++)
if ($i==p)
f=i
print
next
$f=r


and make its executable by chmod +x script.awk and use:



./script.awk p=Field4 r=T input.file





share|improve this answer






















  • You are a life saver. Thank you !!
    – rahul
    Mar 5 '15 at 17:46










  • I ran into another problem while trying your suggestion. Unfortunately I'm not able to add the explanation as a comment, so I`ve edited my question to include details.
    – rahul
    Mar 9 '15 at 13:26







  • 1




    @rahul If you use variant with -F'~' option add -v OFS='~' (Output Field Separator)
    – Costas
    Mar 9 '15 at 15:30











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f188418%2fparse-header-in-a-file-and-based-on-the-header-replace-a-value-in-the-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










awk -F'|' -v p='Field3' -v r='T' '
NR==1
for (i=1;i<=NF;i++)
if ($i==p)
f=i
print
next
$f=r
'


If you like to format it to use like script.awk save it like:



#!/usr/bin/awk -f
BEGIN
OFS=FS="
NR==1
for (i=1;i<=NF;i++)
if ($i==p)
f=i
print
next
$f=r


and make its executable by chmod +x script.awk and use:



./script.awk p=Field4 r=T input.file





share|improve this answer






















  • You are a life saver. Thank you !!
    – rahul
    Mar 5 '15 at 17:46










  • I ran into another problem while trying your suggestion. Unfortunately I'm not able to add the explanation as a comment, so I`ve edited my question to include details.
    – rahul
    Mar 9 '15 at 13:26







  • 1




    @rahul If you use variant with -F'~' option add -v OFS='~' (Output Field Separator)
    – Costas
    Mar 9 '15 at 15:30















up vote
3
down vote



accepted










awk -F'|' -v p='Field3' -v r='T' '
NR==1
for (i=1;i<=NF;i++)
if ($i==p)
f=i
print
next
$f=r
'


If you like to format it to use like script.awk save it like:



#!/usr/bin/awk -f
BEGIN
OFS=FS="
NR==1
for (i=1;i<=NF;i++)
if ($i==p)
f=i
print
next
$f=r


and make its executable by chmod +x script.awk and use:



./script.awk p=Field4 r=T input.file





share|improve this answer






















  • You are a life saver. Thank you !!
    – rahul
    Mar 5 '15 at 17:46










  • I ran into another problem while trying your suggestion. Unfortunately I'm not able to add the explanation as a comment, so I`ve edited my question to include details.
    – rahul
    Mar 9 '15 at 13:26







  • 1




    @rahul If you use variant with -F'~' option add -v OFS='~' (Output Field Separator)
    – Costas
    Mar 9 '15 at 15:30













up vote
3
down vote



accepted







up vote
3
down vote



accepted






awk -F'|' -v p='Field3' -v r='T' '
NR==1
for (i=1;i<=NF;i++)
if ($i==p)
f=i
print
next
$f=r
'


If you like to format it to use like script.awk save it like:



#!/usr/bin/awk -f
BEGIN
OFS=FS="
NR==1
for (i=1;i<=NF;i++)
if ($i==p)
f=i
print
next
$f=r


and make its executable by chmod +x script.awk and use:



./script.awk p=Field4 r=T input.file





share|improve this answer














awk -F'|' -v p='Field3' -v r='T' '
NR==1
for (i=1;i<=NF;i++)
if ($i==p)
f=i
print
next
$f=r
'


If you like to format it to use like script.awk save it like:



#!/usr/bin/awk -f
BEGIN
OFS=FS="
NR==1
for (i=1;i<=NF;i++)
if ($i==p)
f=i
print
next
$f=r


and make its executable by chmod +x script.awk and use:



./script.awk p=Field4 r=T input.file






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 5 '15 at 18:14

























answered Mar 5 '15 at 17:29









Costas

12.6k1129




12.6k1129











  • You are a life saver. Thank you !!
    – rahul
    Mar 5 '15 at 17:46










  • I ran into another problem while trying your suggestion. Unfortunately I'm not able to add the explanation as a comment, so I`ve edited my question to include details.
    – rahul
    Mar 9 '15 at 13:26







  • 1




    @rahul If you use variant with -F'~' option add -v OFS='~' (Output Field Separator)
    – Costas
    Mar 9 '15 at 15:30

















  • You are a life saver. Thank you !!
    – rahul
    Mar 5 '15 at 17:46










  • I ran into another problem while trying your suggestion. Unfortunately I'm not able to add the explanation as a comment, so I`ve edited my question to include details.
    – rahul
    Mar 9 '15 at 13:26







  • 1




    @rahul If you use variant with -F'~' option add -v OFS='~' (Output Field Separator)
    – Costas
    Mar 9 '15 at 15:30
















You are a life saver. Thank you !!
– rahul
Mar 5 '15 at 17:46




You are a life saver. Thank you !!
– rahul
Mar 5 '15 at 17:46












I ran into another problem while trying your suggestion. Unfortunately I'm not able to add the explanation as a comment, so I`ve edited my question to include details.
– rahul
Mar 9 '15 at 13:26





I ran into another problem while trying your suggestion. Unfortunately I'm not able to add the explanation as a comment, so I`ve edited my question to include details.
– rahul
Mar 9 '15 at 13:26





1




1




@rahul If you use variant with -F'~' option add -v OFS='~' (Output Field Separator)
– Costas
Mar 9 '15 at 15:30





@rahul If you use variant with -F'~' option add -v OFS='~' (Output Field Separator)
– Costas
Mar 9 '15 at 15:30


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f188418%2fparse-header-in-a-file-and-based-on-the-header-replace-a-value-in-the-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?