How to insert header with underline?

How to insert header with underline
AM able to insert only header not underline

 sed '1i NAME COUNTRY'  test.txt
 

input file

 UK   1234
USA  2354
AUS  2253
IND  4256
 

Output file

 NAME COUNTRY_CODE
---- ------------
UK   1234
USA  2354
AUS  2253
IND  4256
  
 
sed '1i NAME COUNTRY\n---- ------------' file
NAME COUNTRY
---- ------------
 UK   1234
USA  2354
AUS  2253
IND  4256

or

sed -e '1i NAME COUNTRY' -e '1i ---- ------------' file
1 Like

Hi.

Our shop needed that ability enough that we created a utility for it. We currently are not publishing our codes, but here are how it works and a few additional solutions:

#!/usr/bin/env bash

# @(#) s1       Demonstrate string underlining solutions.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C emphasize align

FILE=${1-data1}

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
pl " Input data file $FILE:"
head $FILE

pl " Results, custom (local) function as single string:"
emphasize -p $FILE

pl " Results, custom (local) function as separate strings:"
emphasize -p -i -s " " $FILE

pl " Results, custom (local) function as separate strings, aligned:"
emphasize -p -i -s " " $FILE |
align -a lr

pl " See solutions at:
https://unix.stackexchange.com/questions/38630/formatting-the-output-underlining"
pl " Results, form underlines, separately:"
word=$( head -n 1 $FILE )
underlines=${word//?/_}
printf "%s\n" "$underlines"
# printf "%s\n" "$word" "$underlines"

pl " Results, function, print with choice of emphasis character:"
print_underline() { echo $1; echo "${1//?/${2:--}}"; }
print_underline "$word" "+"

pl " Results, function, print default emphasis character:"
print_underline "$word" 

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.9 (jessie) 
bash GNU bash 4.3.30
emphasize (local) 1.8
align 1.7.0

-----
 Input data file data1:
NAME COUNTRY_CODE
UK   1234
USA  2354
AUS  2253
IND  4256

-----
 Results, custom (local) function as single string:
NAME COUNTRY_CODE
-----------------
UK   1234
USA  2354
AUS  2253
IND  4256

-----
 Results, custom (local) function as separate strings:
NAME COUNTRY_CODE
---- ------------ 
UK   1234
USA  2354
AUS  2253
IND  4256

-----
 Results, custom (local) function as separate strings, aligned:
NAME COUNTRY_CODE
---- ------------
UK           1234
USA          2354
AUS          2253
IND          4256

-----
 See solutions at:
https://unix.stackexchange.com/questions/38630/formatting-the-output-underlining

-----
 Results, form underlines, separately:
_________________

-----
 Results, function, print with choice of emphasis character:
NAME COUNTRY_CODE
+++++++++++++++++

-----
 Results, function, print default emphasis character:
NAME COUNTRY_CODE
-----------------

There are a number of ways to approach this, such as having the header on a separate file, then apply the underline, and cat that and the body of the data together. Another way might be to keep the header in the data file, remove it , copy to a file with head, underline, etc. (Our utility can insert in place, print only the underline segment, etc. Ours is a perl script, but it could be good practice to do it as a shell script.)

Best wishes ... cheers, drl

Thanks to all......:slight_smile:

Or let sed do the global "-" substitution:

sed '1{ x; s/.*/NAME COUNTRY_CODE/; p; s/[^ ]/-/g; G; }' test.txt

In line 1, save to H-buffer, put the title, print, substitute all non space with "-"; append H-buffer.

1 Like

Thanks a lot its working fine giving out correct Have basic knowledge of shell script.
1)sed ' ' ? and g ,G
2)sed {} ? or differeance or else u can ignore