$ awk -v OFS=',' '/^Name/ { if (line != "") print line; line = $0; next } { line = line OFS $0 } END { if (line != "") print line }' file Name= Garen,Class= 9C,School= US Name= Lulu,Class= 4A Name= Kata,Class= 10D,School= UK
С обновленным вводом в вопросе это дает
Name= Garen,Class= 9C,Last Name= Wilson,School= US, Name= Lulu,Class= 4A,Last Name= Miller, Name= Kata,Class= 10D,School= UK,Last Name= Thomas
Если вы хотите удалить бит
Last Name
, игнорируйте его явно в кодеawk
:$ awk -v OFS=',' '/^Last Name/ { next } /^Name/ { if (line != "") print line; line = $0; next } { line = line OFS $0 } END { if (line != "") print line }' file Name= Garen,Class= 9C,School= US, Name= Lulu,Class= 4A, Name= Kata,Class= 10D,School= UK
Код
awk
в виде отдельнойawk
программы с комментариями:BEGIN { # Set output field separator to a comma. # This can also be done with -v OFS="," on the command line. OFS = "," } /^Last Name/ { # Ignore these lines next } /^Name/ { # A line starts with "Name". # Print the accumulated line and reset the line variable. # Continue immediately with next line of input. if (line != "") print line line = $0 next } { # Accumulate lines in the line variable. # Delimit each input data with OFS (a comma). line = line OFS $0 } END { # Print the last accumulated line. if (line != "") print line }
С
sed
(это почти идентичное решение из ответа на другой вопрос)/^Last Name/ d; # ignore these lines /^Name/ b print_previous; # print previous record H; # append this line to hold space $ b print_previous; # print previous (last) record d; # end processing this line :print_previous; # prints a record accumulated in the hold space x; # swap in the hold space /^$/ d; # if line is empty, delete it s/\n/,/g; # replace embedded newlines by commas # (implicit print)
Запуск:
$ sed -f script.sed file Name= Garen,Class= 9C,School= US Name= Lulu,Class= 4A Name= Kata,Class= 10D,School= UK
Возможным решением может быть использование переменной оболочки CDPATH
. Он содержит набор имен каталогов с разделителями :
-, в которых команда cd
будет искать каталоги.
Например, вы можете установить эту переменную как
CDPATH=".:$HOME/Documents"
.
в начале гарантирует, что cd somename
всегда будет пытаться перейти с cd
на somename
в текущем каталоге , если в текущем каталоге есть каталог с таким именем.
$HOME/Documents
означает, что если имя каталога, с которым вы используете cd
, не найдено в текущем каталоге, то оно ищется в$HOME/Documents
(каталоге Documents
в вашем домашнем каталоге ).
Это означает, что вы легко можете cd
попасть в подкаталог homework
каталога $HOME/Documents
из любого места с помощью
cd homework
... при условии, что такой каталог есть там, а не в текущем каталоге.
Переменная CDPATH
никогда не должна экспортироваться!