/\\section{/ { oldline=$0 if ( oldline ~ /}/ ) { lt=gensub(".*{([^}]+)}.*","\\1","g",oldline) } else { lt=gensub(".*{(.*)","\\1","g",oldline) getline tmp while ( tmp !~ "}" ) { lt=lt "\n" tmp getline tmp } lt=lt "\n" gensub("}.*","","g",tmp) } getline while ( $0 !~ /\\sectionmark/ ) { getline } printf("\\section[%s]{%s%s}\n",lt,lt,$0) getline } ! /\\section{/ { print }