Find the answer to your Linux question:
Results 1 to 8 of 8
I'm trying to add headers to a file. The headers would be "Username User ID Group". I've been trying to use this script but its not working. Can anyone help ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2005
    Posts
    12

    Need help adding headers???


    I'm trying to add headers to a file. The headers would be "Username User ID Group".

    I've been trying to use this script but its not working. Can anyone help me with this?

    awk 'BEGIN{print "USER Name User ID Home Directory" }'

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    I'm a little confused...

    You want to print those 3 to a file?

    Code:
    echo "USER Name\tUser ID\t Home Directory" > file
    If I'm entirely wrong in your process, lemme know.

  3. #3
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    uhhhh - Before you try out Cabhan's idea -- let me ask you some questions...

    Are you wanting to add a header to an already-existing file (with data already in it), or create a file (with header) to which you will afterwards add more data?

    Are you wanting to do this "one time", or are you trying to set up something to place the header in a file as it is generated (from a program)?

    (No offense to Cabhan, but...)
    If you try the 'echo' command on an existing file (with data already in it), it will get over-written (you will loose your data)...

    It would be very helpful if you can describe in more detail what you are trying to do...

  4. #4
    Just Joined!
    Join Date
    Nov 2005
    Posts
    12
    Thanx for replying to my help question and trying to help me out. What I'm trying to do is that I already have a file called Project1 with information in it like so.

    hanak:##chanak:10665:250::/home/home1/summer/chanak:/bin/bash
    ling:##yling:9033:310::/home/home1/ugrad/yling:/bin/bash
    sorgent:##tsorgent:10405:250::/home/home2/grads/sorgent:/bin/bash

    That information is already in the file. What I want to do is add the headers ontop of that information without deleting the information that is in the file. Adding this to the top of it "USER Name User ID Home Directory" and sperating it accordingly. I know I need to use awk and BEGIN but can't get it to work. Really appriciate the help.

  5. #5
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    Are you wanting to do this "one time", or are you trying to set up something to place the header in a file as it is generated (from a program)?

  6. #6
    Just Joined!
    Join Date
    Nov 2005
    Posts
    12
    Just one time I want this to run. You to wrtie the code for it to happen. One time only.

  7. #7
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    If this is a one-time thing, why don't you just open the file with your favorite editor, add the header, save, and exit...

    (Is this homework? --- )

  8. #8
    Just Joined!
    Join Date
    Nov 2005
    Posts
    12
    Can't add headers like that. I already figured it out. Thanx for the help though.

    sed -e 's/:/ /g' -e 's/grads/others/g' -e 's/ugrad/another/g' ~/Desktop/aprojectfile.txt | awk '
    BEGIN {print "USER Name ", "User ID ", "Home Directory" ;}
    {print $1," ", $3, " ", $5 ;}' > projectA4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •