Issue
I have over 10000 of such files and I am trying to make them as a template
my strings are like this
"MLKT_3C_AAAU_01A"
"MLKT_3C_AALI_01A"
"MLKT_3C_AALJ_01A"
"MLKT_3C_AALK_01A"
"MLKT_4H_AAAK_01A"
I am trying to convert them to this
names(MLKT_3C_AAAU_01A)[2] <- '3C_AAAU_01A' df<- full_join(df,MLKT_CS_4942_01A, by = 'V1')
names(MLKT_3C_AALI_01A)[2] <- '3C_AALI_01A' df<- full_join(df,MLKT_3C_AALI_01A, by = 'V1')
names(MLKT_3C_AALJ_01A)[2] <- '3C_AALJ_01A' df<- full_join(df,MLKT_3C_AALJ_01A, by = 'V1')
names(MLKT_3C_AALK_01A)[2] <- '3C_AALK_01A' df<- full_join(df,MLKT_3C_AALK_01A, by = 'V1')
names(MLKT_4H_AAAK_01A)[2] <- '4H_AAAK_01A' df<- full_join(df,MLKT_4H_AAAK_01A, by = 'V1')
The best way I came across until now was to use a text editor and make them one by one. I am wondering if there is a way in bash to get the above strings and convert it to the example I provided ?
before I start, I remove quotation from each line
sed 's/\"//g' example.txt > exampleout.txt
AT first I try to add names(
at the beging of each line . so lets imagine my file which has all those strings per line is called exampleout.txt. which gives me three time names( instead once
awk '$0="names("$0' exampleout.txt > myout.txt
Then I try to paste )[2] <- '' df<- full_join(df,, by = 'V1')
at the end of each line using the following
sed -e 's/$/)[2] <- '' df<- full_join(df,, by = 'V1') /' myout.txt > myout2.txt
so it led me to this
names(MLKT_3C_AAAU_01A )[2] <- df<- full_join(df,, by = V1)
names(MLKT_3C_AALI_01A)[2] <- df<- full_join(df,, by = V1)
names(MLKT_3C_AALJ_01A )[2] <- df<- full_join(df,, by = V1)
names(MLKT_3C_AALK_01A)[2] <- df<- full_join(df,, by = V1)
names(MLKT_4H_AAAK_01A)[2] <- df<- full_join(df,, by = V1)
Solution
$ awk -F'"' '{
x=$2; sub(/^[^_]+_/,"",x)
printf "names(%s)[2] <- \047%s\047 df<- full_join(df,%s, by = \047V1\047)\n", $2, x, $2
}' file
names(MLKT_3C_AAAU_01A)[2] <- '3C_AAAU_01A' df<- full_join(df,MLKT_3C_AAAU_01A, by = 'V1')
names(MLKT_3C_AALI_01A)[2] <- '3C_AALI_01A' df<- full_join(df,MLKT_3C_AALI_01A, by = 'V1')
names(MLKT_3C_AALJ_01A)[2] <- '3C_AALJ_01A' df<- full_join(df,MLKT_3C_AALJ_01A, by = 'V1')
names(MLKT_3C_AALK_01A)[2] <- '3C_AALK_01A' df<- full_join(df,MLKT_3C_AALK_01A, by = 'V1')
names(MLKT_4H_AAAK_01A)[2] <- '4H_AAAK_01A' df<- full_join(df,MLKT_4H_AAAK_01A, by = 'V1')
Answered By - Ed Morton