printig 20 lines from string

Posted on 2008-11-13
Last Modified: 2013-12-26
hi  I need to write an awk script that prints 20 lines from a string and directs output to a file.
pls help.
Question by:mbpssgms
    LVL 14

    Expert Comment

    Can you give an example of the input and desired output?
    LVL 48

    Expert Comment

    How does your string store 20 lines?
    LVL 20

    Expert Comment

    strings in shell scripts do not have \n or \r or anything like that in tham as far as I know.

    If you have managed to get new lines into a string then you could echo it and then pipe it to head and then pipe that into a file

    echo "$string" | head -n 20 > /path/file.txt

    LVL 14

    Expert Comment

    The question needs clarification.  At this point all we could do is guess at the i/o.
    LVL 51

    Accepted Solution

    assuming string (as asked for in the original question) is in file named string.txt:

    head -20 string.txt>file
    tail -20 string.txt>file
    sed -ne '1,20p' string.txt>file
    awk '(NR<21){print}' string.txt>file
    awk 'END{printf"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}' string.txt>file

    Note: only the last examples ensures that you realy get 20 lines.

    Author Closing Comment

    thanks I'd been away.  This was similar to another solution.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    This article will show, step by step, how to integrate R code into a R Sweave document
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    The viewer will learn how to count occurrences of each item in an array.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now