I am in the situation where I have a text file and I need to find the string that matches a specific pattern and modify it twice to have two different outputs, something like the following:
let's say this is the original file:
ID_00:/hdfs_01 name1 ID_01:/hdfs_02 name2 ID_02:/hdfs_03 name3 ID_03:/hdfs_app_data_01 name4 ID_04:/hdfs_app_data_02 name5 ID_05:/hdfs_cmmd_prt name6
you will find that it is similar to the structure of an fstab.
And the expected result is :
ID_00:/hdfs_01 name1_old ID_06:/hdfs_01 name1 ID_01:/hdfs_02 name2_old ID_07:/hdfs_02 name2 ID_02:/hdfs_03 name3_old ID_08:/hdfs_03 name3 ID_03:/hdfs_app_data_01 name4_old ID_09:/hdfs_app_data_01 name4 ID_04:/hdfs_app_data_02 name5_old ID_10:/hdfs_app_data_02 name5 ID_05:/hdfs_cmmd_prt name6_old ID_11:/hdfs_cmmd_prt name6
So far, I've considered doing a simple sed search for hdfs as is the only thing in common, add it to a different text file, modify it, and then adding the entire modified block back to the source file.
Do this twice, once to add the old suffix and once to modify the ID.
Something like:
sed -n '/hdfs/p' >> new.file
Add the old suffix in the new.file with sed/awk save the result in a different file.
Now do it again to update the ID and save the result in a different file.
Then with those 2 files created with the expected output I can easily remove the record on the current file, add the new ones and reload the app.
this is more or less the plan so far.
But I would like to know if this could be achieved using only sed and its buffer.
Saving the original matched pattern, modify it once for the old suffix and print it [this is output #1] and then modify the original line again for the ID and print it again [this is output #2 ], then we can remove the original line.
UPDATE:
The ID_### is a fixed value. Has no progression and is not captured arithmetically. In the example, it seems to have progression but it doesn't.
Then it should look something like this:
ID_00:/hdfs_01 name1_old ID_25:/hdfs_01 name1 ID_01:/hdfs_02 name2_old ID_10:/hdfs_02 name2 ID_02:/hdfs_03 name3_old ID_56:/hdfs_03 name3
The value is selected as per the maatching string.
Where, for example: hdfs_01 will definitely carry the ID ID_25, but /hdfs_02 will carry the ID_10.
This value will be determined at run time.
We also have some other values inside that we should not modify like:
ID_00:/hdfs_01 name1 ID_01:/hdfs_02 name2 ID_20:/DSF_03 name3 ID_21:/TEST_05 name3
So the result will look like:
ID_00:/hdfs_01 name1_old ID_25:/hdfs_01 name1 ID_01:/hdfs_02 name2_old ID_10:/hdfs_02 name2 ID_20:/DSF_03 name3 ID_21:/TEST_05 name3