将所有内容保留到最后一列但删除最后一列中最后一个空格之前的字符

时间:2017-06-03 17:58:21

标签: bash shell unix

如何删除最后一列中的所有字符到最后一个空格?

<?php 

get_header();

?> 

<!-- recipe -->
<section class="recipe-wrap">

	<?php
	/*
	 * Loop through Categories and Display Posts within
	 */
	$post_type = 'recipe';
	$category_link = get_category_link($cat->cat_ID);
	 
	// Get all the taxonomies for this post type
	$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
	 
	foreach( $taxonomies as $taxonomy ) :
	 
	    // Gets every "category" (term) in this taxonomy to get the respective posts
	    $terms = get_terms( $taxonomy );
	 
	    foreach( $terms as $term ) : ?>

	<div class="recipe-category owl-carousel-slide">
		<div class="row">

			<h2><?php echo $term->name; ?><a href="#">see all</a></h2>
	        
	        <div class="recipe-category-carousel owl-carousel owl-theme">

	        	<?php
		        $args = array(
		                'post_type' => $post_type,
		                'posts_per_page' => 10,  //show all posts
		                'tax_query' => array(
		                    array(
		                        'taxonomy' => $taxonomy,
		                        'field' => 'slug',
		                        'terms' => $term->slug,
		                    )
		                )
		 
		            );
		        $posts = new WP_Query($args);
		 
		        if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>

	            <div class="item recipe-box">

	            	<a href="<?php the_permalink(); ?>">
	                	<img src="<?php echo(types_render_field('artwork', array('raw' => true) )); ?>">
	                	<p><?php the_title(); ?></p>
	                </a>
	            </div> 

                <?php endwhile; endif; ?>
			        </div>
			       
			        </section>
			 
			    <?php endforeach;
			 
			endforeach; ?>

	        </div>

        </div>
    </div>
</section>
<!-- /recipe -->

<?php 

get_footer();

?>

结果如下:

20160101|193.150.22.1|57381|0.0.0.0/0|57381 42708 29468 41136 41136 41136 3292

1 个答案:

答案 0 :(得分:0)

您可以使用sed

来实现此目的
echo "20160101|193.150.22.1|57381|0.0.0.0/0|57381 42708 29468 41136 41136 41136 3292" | sed -e 's/\(|.*|\).* \(.*\)/\1\2/'

给出:

20160101|193.150.22.1|57381|0.0.0.0/0|3292

修改

如果您想仅使用bash内部功能获得结果,可以使用bash substring removal

s="20160101|193.150.22.1|57381|0.0.0.0/0|57381 42708 29468 41136 41136 41136 3292"
echo "${s%|*}|${s##* }"

也会得到理想的结果。