Insert a pipe separated header to a large txt file [duplicate] - unix

This question already has answers here:
Add a header to a tab delimited file
(8 answers)
Closed 6 years ago.
I have pipe separated large file(s) that has more 10,000,000 lines. All of these files are missing header and only the first split has the header. I want insert the header line for the rest of the files.
I tried following:
sed -i '1 i \user_id|name|age|transactions' file.txt
but this didn't work. What is the most efficient way to insert a header line for large file.
Similar question has been asked, unfortunately solution didn't work. It could be due to sed structure of understanding pipe separation might be different.

Try this:
sed '1s/.*/user_id|name|age|transactions\n&/' file.txt

Related

Unix substitute multiple strings using a reference file [duplicate]

This question already has answers here:
Match exact word with awk on Mac OS X
(4 answers)
Closed 11 months ago.
I have a reference file and using that I want to replace multiple files in a directory. I am using AWK GSUB for that, however it is not replacing exact word, but replacing all occurrences. How can I stop that behaviour? How can I replace just the word? in this case the word is "IT"
My reference file
$ cat dev_to_prod.config
nonprod_DATA_PATH PROD_DATA_PATH
nonprod_ENCRYPTKEY PROD_ENCRYPTKEY
IT Business
My current data file
$ cat filefile.txt
IT
WITH
/IT/DFGh/erfe
/WITH/IT/sjfgh/hjIT/dfdsf/ITvjkl
Output with current code
awk 'FNR==NR{A[$1]=$2;next}{for(i in A)gsub(i,A[i])}1' dev_to_prod.config file.txt
Business
WBusinessH
/Business/DFGh/erfe
/WBusinessH/Business/sjfgh/hjBusiness/dfdsf/Businessvjkl
man awk says:
\< matches the empty string at the beginning of a word.
\> matches the empty string at the end of a word.
Then would you please try:
awk 'FNR==NR{A[$1]=$2;next}{for(i in A)gsub("\\<"i"\\>",A[i])}1' dev_to_prod.config file.txt
Output:
Business
WITH
/Business/DFGh/erfe
/WITH/Business/sjfgh/hjIT/dfdsf/ITvjkl

There is a file some where in your system which contains word "UnixCommandInterviewQuestions” How will find that file in Unix? [duplicate]

This question already has answers here:
How to find all files containing specific text (string) on Linux?
(54 answers)
Closed 5 years ago.
This is a common $unix question thats asked in interviews. I know that you have to use find command but what is the exact answer? i searched in google but couldnt find a proper answer. below command i tried but its wrong.
$find / -name "UnixCommandInterviewQuestions”
Make sure you read the question very carefully.
It says that the file contains "UnixCommandInterviewQuestions", not that the file name is "UnixCommandInterviewQuestions".
One correct answer would be a recursive grep command, like:
grep -r UnixCommandInterviewQuestions /
There are other solutions that may be faster.

unix sed and redirection [duplicate]

This question already has answers here:
Find and replace in file and overwrite file doesn't work, it empties the file
(12 answers)
Closed 6 years ago.
I am trying to modify a file using sed in a linux server (Ubuntu 16.04).
Here is an example of the code I am running:
sed 's/lineToChange/newString/' example.txt > example.txt
I feel like I should see newString in example.txt after executing this command since the result of the sed command (which prints newString when executed by itself without the redirect) is redirected to overwrite the example.txt content.
Unfortunately the file ends up empty when I do this...
My common sense is telling me that this should be right but clearly there is something I just don't understand here.
If you want to edit a file inline, you should use the -i option:
sed -i 's/lineToChange/newString/' example.txt
This runs sed into a new file and moves that file to example.txt. Whenever you do ">", you essentially empty out example.txt which makes it empty for sed to work on.

AWK to check a string pattern and extract it from a file [duplicate]

This question already has answers here:
How to print matched regex pattern using awk?
(9 answers)
How to print regexp matches using awk? [duplicate]
(3 answers)
Closed 7 years ago.
Below are the file contents:
{30001002|XXparameter|XSD_LOC|$\{FILES_DIR\}/xsd/EDXFB_mbr_demo.xsd|3|2|$|#{0|}}
{30001002|XXparameter|source_files|$XSD/EDXFB_mbr_demo.xsd|3|1|l|#{0|}}
I trying to accomplish below using awk:
Firstly I want to search for string Pattern "EDXFB*.xsd".
If exists, then extract the strings that starts with "EDXFB" and ends with ".xsd"
Output:
EDXFB_mbr_demo.xsd
EDXFB_mbr_demo.xsd
The basic awk pattern to extract the expression and print out matched data is following:
gawk 'match($0, /EDXFB.+\.xsd/, a) { print a[0] }'
Though, you should really spend some time reading awk manual.
And the regular expression could be changed to /EDXFB[a-z_]+\.xsd/ if it contains only lower-cased characters and _.
[EDIT]: Updated with cleaner code from #JID. Thanks :)
Here is one way to do it:
awk -F/ '/EDXFB.*\.xsd/ {split($NF,a,"|");print a[1]}' file
EDXFB_mbr_demo.xsd
EDXFB_mbr_demo.xsd
It separate the line by / then print last field until |
In your example, probably grep would do what you want:
grep -o 'EDXFB.*\.xsd'

find and replace using sed in unix [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Use slashes in sed replace
I need to find the following string /a/b/c and replace it with /r/s/t
It is a string and not a folder
/sam/pam/nancy --> /tim/cook/iphone
I am in the directory and just need to update multiple files having this line.
Use sed to change the files in-place. For example:
sed -i 's|/a/b/c|/r/s/t|g' *.txt
perl -pi -e 's/\/a\/b\/c/\/r\/s\/t/g' file_name

Resources