How to insert header with underline?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert header with underline?
# 1  
Old 12-06-2017
How to insert header with underline?

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

Code:
 sed '1i NAME COUNTRY'  test.txt

input file
Code:
 UK   1234
USA  2354
AUS  2253
IND  4256

Output file
Code:
 NAME COUNTRY_CODE
---- ------------
UK   1234
USA  2354
AUS  2253
IND  4256

# 2  
Old 12-06-2017
Code:
sed '1i NAME COUNTRY\n---- ------------' file
NAME COUNTRY
---- ------------
 UK   1234
USA  2354
AUS  2253
IND  4256

or
Code:
sed -e '1i NAME COUNTRY' -e '1i ---- ------------' file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-06-2017
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:
Code:
#!/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:
Code:
$ ./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

Last edited by drl; 12-06-2017 at 02:30 PM..
# 4  
Old 12-06-2017
Thanks to all......Smilie
# 5  
Old 12-06-2017
Or let sed do the global "-" substitution:
Code:
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.
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 12-06-2017
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

Last edited by Kalia; 12-06-2017 at 04:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert date/time header at top of file

I'm trying to take mrt output and put it at the top of a file along with the date and time. I was able to do it at the bottom of the file with the following printf "********** $(date) **********\n\n" >> $OUTPUT_PATH/$HOSTNAME mtr -r -w -c 10 $HOSTADDRESS >> $OUTPUT_PATH/$HOSTNAME printf... (2 Replies)
Discussion started by: kramer65
2 Replies

2. Shell Programming and Scripting

Insert a header record (tab delimited) in multiple files

Hi Forum. I'm struggling to find a solution for the following issue. I have multiple files a1.txt, a2.txt, a3.txt, etc. and I would like to insert a tab-delimited header record at the beginning of each of the files. This is my code so far but it's not working as expected. for i in... (2 Replies)
Discussion started by: pchang
2 Replies

3. Shell Programming and Scripting

Help with underline text based on specific region

Input file 2 5 ASFGEWTEWRQWEQ 10 20 QEWIORUEIOUEWORUQWEQWRQRQWGQWGFQ 1 6 WRQTQWTQTQWTQT Desired output file 2 5 ASFGEWTEWRQWEQ 10 20 QEWIORUEIOUEWORUQWEQWRQRQWGQWGFQ 1 6 WRQTQWTQTQWTQT Column 1 is the start region of underline the text in column 3; Column 2 is the end region of... (13 Replies)
Discussion started by: cpp_beginner
13 Replies

4. Shell Programming and Scripting

Bold and Underline - not displyed in more/less/vi

I have a script main.shl which has few lines like this #bold tput smso echo "\t\tsome statement\t\t" tput rmso I am executing the main.shl from the shell and redirected its output to a separate file like this $main.shl >main.log 2>&1 & once after running this script, if I "cat" the... (0 Replies)
Discussion started by: ramkrix
0 Replies

5. Shell Programming and Scripting

Insert Header Name

Can anyone help. I have sql to CSV showing header with ALIAS names but I want to generate CSV file with user friendly name i.e from AIAN8 to Adress Book Number etc. SELECT AIAN8 || , || F0101.ABALPH || , || AICO || showing following below in CSV output intead of above Address... (4 Replies)
Discussion started by: s1a2m3
4 Replies

6. Shell Programming and Scripting

insert a header in a huge data file without using an intermediate file

I have a file with data extracted, and need to insert a header with a constant string, say: H|PayerDataExtract if i use sed, i have to redirect the output to a seperate file like sed ' sed commands' ExtractDataFile.dat > ExtractDataFileWithHeader.dat the same is true for awk and... (10 Replies)
Discussion started by: deepaktanna
10 Replies

7. UNIX for Dummies Questions & Answers

insert header row into .xls

Hello, I am building an .xls file extracting info from a DB to be eventually emailed. All is good except how do I put in a header row.. like date, name of report etc. before the columns with the actual column name and data? Thanks for any assistance.. the below is after I have signed into... (11 Replies)
Discussion started by: Tish
11 Replies

8. Shell Programming and Scripting

Underline

i want to print underline under a text using shell or awk.can any body help me regarding this problem? i hav tried with echo -e -n "\033$4}' like expected output is 123 456 12 122 567 13 211 087 14 311 987 15 like the avove Thank you regards, Pankaj. (2 Replies)
Discussion started by: panknil
2 Replies

9. UNIX for Dummies Questions & Answers

How to underline/bold and how to align output

Hi, I work with AIX 5 and have two basic questions: 1) How do I underline/bold a word in a text output? Any way to do it with echo command? basic example: echo "FOLDER " >> folder.txt ( I wish the word FOLDER to be underlined and bold). 2) Suppose I have the following pipe delimited... (1 Reply)
Discussion started by: clara
1 Replies

10. UNIX for Dummies Questions & Answers

underline character in vi editor

I want to print a man page for a command in unix, this is what I did man command > command.txt but when I view the output file command.txt I found there are lot of _^H characters that in the man page are actually underline character, how can I replace this _^H with underline character? ... (2 Replies)
Discussion started by: Melissa
2 Replies
Login or Register to Ask a Question