I want to add $x/ from data formatted as $x-xyz (xyz is a mix of text and number to be ignored).Example dataAB-10CCD-02EOutputAB/AB-10CCD/CD-02EHow do I describe the variable as text before the hyphen? My idea is:define $x = # don't know how to do that## and then somehow use sed to prependsed -i s/$/$x/ file# I don't know if sed can handle variables though - the above one liner is from Googling
Prepend x/ to line where x is a variable on each line
bash;sed
Here's a quick and dirty awk version:awk -F- '{print $1/$0}' input_file > output_fileWhat it does is use - as a field separator, and prints the first column (i.e. everything before the first -), then a /, then the whole original line.A way of doing the same thing with sed would be:sed -e 's;^\([^-]*\)\(.*\);\1/\1\2;' input_file > output_file(but that's hardly readable.)If you want to do it in plain bash, you can use string manipulations:$ foo=AB-10C$ prefix=${foo%%-*}$ echo ${prefix}/${foo}AB/AB-10CUse that in a while read loop or similar if the data is coming from a file.