-2

I am new to unix commands, please forgive if i am not using correct line of code below.

I have files (xxxx.txt.date) on winscp with header and footer. Now i want to add N number of pipe (|) at the end of the each row of all files starting from 2nd line till second last line. (i dont want | in header as well as footer).

Now i have created a scirpt in which i am using below command:

sed  -e "2,\$s/$/|/" $file | column -t

2,$s/$/|/: adds | at the end of every line from line 2 Now below are the issues i am facing

First

  • The data doesn't change in the files i am able to see pipe added at end of each row in hive, how can i change data in files?
  • I don't want | in footer.

Any suggestion or help will be appreciated.

Thanks in advance !!

Finn
  • 166
  • 2
  • 10
amitesh
  • 758
  • 5
  • 16
  • 36
  • i am able to move data into file like sed -e "2,\$s/$/|/" $file $updated_file no i just want to remove | from footer – amitesh Aug 19 '20 at 10:45
  • who ever put it down means they dont want any developer who is just starting coding on something new won't do that is it ? – amitesh Aug 19 '20 at 11:01

1 Answers1

0

If you need to append just one "|" at the end of each line except header and footer

sed -i '1n; $n; s/$/|/' file_name

1n; $n; : Just print first and last line as is.
-i : make changes to the file instead of printing to STDOUT.

If you need to append n pipes at the end of each line except Header and Footer. If you use the below awk command, you will have to redirect the output to a temporary file and then rename it.

Assumptions:

  1. I am assuming your Header and Footer are standard and start with some character(e.g., H, F, T etc) or String(Header, Footer, Trailer etc)

  2. I am assuming your original file is delimited with "|". You can specify your actual delimiter in the below awk.

    awk -F'|' -v n=7 '{if(/^Header|^Footer/) {print} else {end="";for (i=1;i<=n;i++) end=sprintf("%s%s", end, "|"); rec=sprintf("%s%s", $0, end); print rec}}' file_name

n=number of times you want to repeat | at the end of each line.
^Header|^Footer - If the line starts with "Header" or "Footer", just print the record as it is. You can specify your header and footer strings from file.
for loop - prepares a string "end" which contains "|" n times.
rec - Contains concatenated string of entire record followed by end string

whirlcano
  • 43
  • 8
  • thanks for the reply.. one query | are also added in header and footer, so you just give and example for header and footer, also footer start with F| then number of rows and header contain columns names so could you please guide me how to print only header and footer .. – amitesh Aug 19 '20 at 13:45